使用 cut 指令沒有獲得所需的輸出?

使用 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 ~]#

我期望輸出中的第二列;但沒有得到任何東西。有任何想法嗎 ?

答案1

對於-d " ",欄位分隔符號是一個(且僅有一個)空格字元。與 shell 分詞相反,cut對空格的處理與任何其他字元沒有任何不同。因此cut -d " " -f2返回""in root   19,就像返回""incut -d: -f2一樣root:::19

您需要擠壓空白才能將任何空間序列轉換為空間:

ps aux | grep ata | tr -s ' ' | cut -d ' ' -f2

或使用awk其預設吐出模式中的 where ,它不使用分隔符,而是拆分為非空白字元序列清單:

ps aux | awk '/ata/{print $2}'

但在這種情況下,您可能需要使用:

pgrep -f ata

或至少:

ps -eo pid= -o args= | awk '/ata/{print $1}'

僅匹配參數。

答案2

使用:

ps aux | awk '/ata/{print $2}'

您使用的命令cut沒有給出所需的輸出,因為列之間有多個空格。

相關內容