AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 791084
Accepted
showkey
showkey
Asked: 2025-02-15 12:30:27 +0800 CST2025-02-15 12:30:27 +0800 CST 2025-02-15 12:30:27 +0800 CST

为什么不能在 df 的输出中添加标题?

  • 772

获取名称包含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
-----      --------------

沒有排序線!!!

bash
  • 5 5 个回答
  • 96 Views

5 个回答

  • Voted
  1. Best Answer
    user10489
    2025-02-15T13:27:33+08:002025-02-15T13:27:33+08:00

    这行不通,因为管道不是那样工作的。

    该命令df |(head -n1; grep shm)执行以下操作:

    • df 生成一些输出
    • head 接收所有输出,打印第一行,然后停止丢弃读取的其余内容。
    • 没有剩余的输出可供 grep 作为输入。

    即使 head 没有读取所有数据,它也会读取满的缓冲区,几乎永远不会只有一行,而且它无法将剩余的输入返回给下一个命令。这不是管道的工作方式,也不是输入缓冲的工作方式。而且以不同的方式实现它效率不高。因此总会有一些第二个命令看不到的缺失数据,可能会给出不完整的答案。

    另一个答案中的替代命令是执行此操作的正确方法,而不是期望管道中的数据在两个命令之间神奇而完美地分割。

    这是一个更可重复的例子:

    $ yes this is some long text | head -2000 | (head -1 ; wc -l)
    this is some long text
    1644
    

    其工作原理如下:

    • yes 命令重复输出同一行文本
    • 第一个 head 命令将 yes 输出截断为 2000 行
    • () 中的 head 读取一个充满文本的缓冲区并打印第一行
    • wc 命令计算剩余的行数,您可能已经注意到,wc 计算出的行数明显少于 1999 行。

    稍微计算一下,就有 355 行缺失,或者(每行 23 个字符)至少 8165 个字符。

    假设典型的缓冲区大小为 8192,第一行打印了 23 个字符,并且在该缓冲区的最后一行(不完整)的开头又吃了 4 个字符,为 wc 留下了 1644 行,其中第一行很短。

    • 3
  2. Gilles Quénot
    2025-02-15T13:38:59+08:002025-02-15T13:38:59+08:00

    对于原因部分,@user10489 给出了答案。

    使用awk:

     df | awk 'NR==1;/shm/'
    

    NR==1就像head -n1和/shm/就像grep shm

    • 3
  3. Paul_Pedant
    2025-02-15T18:33:26+08:002025-02-15T18:33:26+08:00

    如果awk读取所有输入,则不会遇到缓冲问题。因此,它可以选择性地通过管道将标头之后的所有内容传输到sort管道awk。

    df | awk 'NR == 1 { print; next; } /^.dev/ { print | "sort"; }'
    

    我没有太多shm,但我喜欢按顺序报告我的分区,同样ps输出。

    冗长,但也许值得在 中声明为一个函数.bashrc。

    事实上,bash读取行时无需缓冲,因此下面的方法也可以工作:

    df | { read -r Hdr; printf '%s\n' "$Hdr"; grep '^.dev' | sort; }
    
    • 2
  4. waltinator
    2025-02-15T12:56:15+08:002025-02-15T12:56:15+08:00

    通过使用grep正则表达式,可以:

    df | grep -E 'shm|1K-blocks`
    

    匹配包含 ' shm' 或 ' 1K-blocks' 的行

    • 1
  5. showkey
    2025-02-15T16:47:38+08:002025-02-15T16:47:38+08:00

    1.{ head -2; sort -n; } <raw.txt可以工作,原因如下--重定向和管道。

    2.为什么ps aux |(head -n 1 ;grep [f]irefox)能起作用?
    缓冲区很重要。

    yes this is some long text | head -355 | (head -1 ; wc -l)
    this is some long text
    0
    yes this is some long text | head -356 | (head -1 ; wc -l)
    this is some long text
    0
    yes this is some long text | head -357 | (head -1 ; wc -l)
    this is some long text
    1
    yes this is some long text | head -358 | (head -1 ; wc -l)
    this is some long text
    2
    

    user10489的观点是正确的,“基本上,ps aux 的输出更长,因此它会填满 head 的缓冲区”。

    对于该命令ps aux |(head -n 1 ;grep [f]irefox),看上去是 ps aux输出多行填充多页,head -n 1只读出缓冲区中的第一页,第一页中行号大于 1 的内容将被读出;其他在第一页中没有的信息将传递给grep匹配。
    统计一页的大小:

    yes this is some long text | head -357 | (head -1 ; wc -c)
    this is some long text
    19
    

    此处一行包含 23 个字符:

    yes this is some long text | head -1|wc -c
    23
    

    然后:

    356*23+(23-19)=8192
    

    3.写标题然后添加匹配的行。

    df | head -1 ;(df | grep shm)
    Filesystem     1K-blocks      Used Available Use% Mounted on
    tmpfs            4024488     99660   3924828   3% /dev/shm
    
    • -1

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve