获取名称包含firefox
并排除grep
进程的所有进程,这里显示所有进程是没有意义的,省略了很多行。
ps aux | grep [f]irefox
debian 7069 1.0 4.4 3134148 359168 ? Sl 11:58 0:12 /usr/lib/firefox-esr/firefox-esr
debian 7128 0.0 0.4 223884 36824 ? Sl 11:58 0:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20241118130310 -prefsLen 28341 -prefMapSize 249085 -appDir /usr/lib/firefox-esr/browser {0c853969-95e1-4db0-9e95-eeaee3d4f814} 7069 true socket
输出不包含ps
命令中的标头信息,为了获取标头,head -n1
请在管道后添加。
ps aux |(head -n 1 ;grep [f]irefox)
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
debian 7069 0.9 4.4 3134148 361740 ? Sl 11:58 0:13 /usr/lib/firefox-esr/firefox-esr
debian 7128 0.0 0.4 223884 36824 ? Sl 11:58 0:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20241118130310 -prefsLen 28341 -prefMapSize 249085 -appDir /usr/lib/firefox-esr/browser {0c853969-95e1-4db0-9e95-eeaee3d4f814} 7069 true socket
其他 bash 命令:
df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 3977796 0 3977796 0% /dev
tmpfs 804900 1356 803544 1% /run
/dev/sdb1 460349516 143209832 293681076 33% /
tmpfs 4024488 100444 3924044 3% /dev/shm
tmpfs 5120 16 5104 1% /run/lock
df | grep shm
tmpfs 4024488 101536 3922952 3% /dev/shm
df |(head -n1; grep shm)
Filesystem 1K-blocks Used Available Use% Mounted on
为什么执行时无法得到下面的输出df |(head -n1; grep shm)
?
df |(head -n1; grep shm)
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 4024488 101536 3922952 3% /dev/shm
为什么“ps aux |(head -n 1 ;grep [f]irefox)”中的 grep 可以匹配到行?
正如专家user10489
指出的:
The command df |(head -n1; grep shm) does this:
df generates some output
head takes all of the output, prints the first line, and then quits throwing away all the rest of what it read.
There is no output left for grep to take as input.
另一篇文章可以深入探讨这一点:
cat > raw.txt <<EOF
ID DESCRIPTION
----- --------------
2 item2
4 item4
1 item1
3 item3
EOF
我想要获取以下带标题的排序行:
ID DESCRIPTION
----- --------------
1 item1
2 item2
3 item3
4 item4
Gilles Quénot
得到一个简单的解决方案--简单的解决方案
{ head -2; sort -n; } <raw.txt
当命令中的{}
输入重定向时<raw.txt
,如果head -2;
运行如下user10489
命令:head takes all of the output, prints the first line, and then quits throwing away all the rest of what it read.
为什么sort -n
需要用行进行排序?
结果将是
ID DESCRIPTION
----- --------------
沒有排序線!!!