获取名称包含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
----- --------------
沒有排序線!!!
这行不通,因为管道不是那样工作的。
该命令
df |(head -n1; grep shm)
执行以下操作:即使 head 没有读取所有数据,它也会读取满的缓冲区,几乎永远不会只有一行,而且它无法将剩余的输入返回给下一个命令。这不是管道的工作方式,也不是输入缓冲的工作方式。而且以不同的方式实现它效率不高。因此总会有一些第二个命令看不到的缺失数据,可能会给出不完整的答案。
另一个答案中的替代命令是执行此操作的正确方法,而不是期望管道中的数据在两个命令之间神奇而完美地分割。
这是一个更可重复的例子:
其工作原理如下:
稍微计算一下,就有 355 行缺失,或者(每行 23 个字符)至少 8165 个字符。
假设典型的缓冲区大小为 8192,第一行打印了 23 个字符,并且在该缓冲区的最后一行(不完整)的开头又吃了 4 个字符,为 wc 留下了 1644 行,其中第一行很短。
对于原因部分,@user10489 给出了答案。
使用
awk
:NR==1
就像head -n1
和/shm/
就像grep shm
如果
awk
读取所有输入,则不会遇到缓冲问题。因此,它可以选择性地通过管道将标头之后的所有内容传输到sort
管道awk
。我没有太多
shm
,但我喜欢按顺序报告我的分区,同样ps
输出。冗长,但也许值得在 中声明为一个函数
.bashrc
。事实上,
bash
读取行时无需缓冲,因此下面的方法也可以工作:通过使用
grep
正则表达式,可以:匹配包含 '
shm
' 或 '1K-blocks
' 的行1.
{ head -2; sort -n; } <raw.txt
可以工作,原因如下--重定向和管道。2.为什么
ps aux |(head -n 1 ;grep [f]irefox)
能起作用?缓冲区很重要。
user10489
的观点是正确的,“基本上,ps aux 的输出更长,因此它会填满 head 的缓冲区”。对于该命令
ps aux |(head -n 1 ;grep [f]irefox)
,看上去是ps aux
输出多行填充多页,head -n 1
只读出缓冲区中的第一页,第一页中行号大于 1 的内容将被读出;其他在第一页中没有的信息将传递给grep
匹配。统计一页的大小:
此处一行包含 23 个字符:
然后:
3.写标题然后添加匹配的行。