使用 cut 命令没有获得所需的输出?

Ank*_*kit 4 filter cut

[root@localhost ~]#  ps aux | grep ata
root        19  0.0  0.0      0     0 ?        S    07:52   0:00 [ata/0]
root        20  0.0  0.0      0     0 ?        S    07:52   0:00 [ata_aux]
root      1655  0.0  2.6  22144 13556 tty1     Ss+  07:53   0:18 /usr/bin/Xorg :0 -nr -verbose -auth /var/run/gdm/auth-for-gdm-t1gMCU/database -nolisten tcp vt1
root      3180  0.0  0.1   4312   728 pts/0    S+   14:09   0:00 grep ata
[root@localhost ~]#  ps aux | grep ata | cut -d" " -f 2

[root@localhost ~]#
Run Code Online (Sandbox Code Playgroud)

我希望输出中的第二列;但没有得到任何东西。有任何想法吗 ?

Sté*_*las 7

使用-d " ",字段分隔符是一个(并且只有一个)空格字符。与 shell 分词相反,cut不会将空格与任何其他字符区别对待。所以cut -d " " -f2回报""root   19,就像它会返回""cut -d: -f2root:::19

您需要挤压空白以将任何空间序列转换为一个空间:

ps aux | grep ata | tr -s ' ' | cut -d ' ' -f2
Run Code Online (Sandbox Code Playgroud)

或者awk在默认的吐出模式中使用where,它不使用分隔符而是拆分为非空白字符序列列表:

ps aux | awk '/ata/{print $2}'
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,您可能需要使用:

pgrep -f ata
Run Code Online (Sandbox Code Playgroud)

或者至少:

ps -eo pid= -o args= | awk '/ata/{print $1}'
Run Code Online (Sandbox Code Playgroud)

仅匹配参数。