2012-06-09 115 views
2

例如,刪除一切,除了各行的特定索引的內容

在bash當我鍵入ps -wef我會得到:

UID  PID PPID C STIME TTY   TIME CMD 
root   1  0 0 20:06 ?  00:00:01 /sbin/init 
root   2  0 0 20:06 ?  00:00:00 [kthreadd] 
root   3  2 0 20:06 ?  00:00:00 [ksoftirqd/0] 
root   6  2 0 20:06 ?  00:00:00 [migration/0] 
root   7  2 0 20:06 ?  00:00:02 [watchdog/0] 
root   8  2 0 20:06 ?  00:00:00 [cpuset] 
root   9  2 0 20:06 ?  00:00:00 [khelper] 
root  10  2 0 20:06 ?  00:00:00 [kdevtmpfs] 
root  11  2 0 20:06 ?  00:00:00 [netns] 
root  12  2 0 20:06 ?  00:00:00 [sync_supers] 
root  13  2 0 20:06 ?  00:00:00 [bdi-default] 
root  14  2 0 20:06 ?  00:00:00 [kintegrityd] 
root  15  2 0 20:06 ?  00:00:00 [kblockd] 
root  16  2 0 20:06 ?  00:00:00 [ata_sff] 
root  17  2 0 20:06 ?  00:00:00 [khubd] 
root  18  2 0 20:06 ?  00:00:00 [md] 
root  21  2 0 20:06 ?  00:00:00 [khungtaskd] 

,我知道我想要的列保持,例如我想保持第二列是PID列,最後的結果將是:

1 2 3 6 7 8 9 10 11 12 13 14 15 16 17 18 21 

PS的原始結果的第一行就會被刪除。

如何使用sed或awk來獲得我的最終結果?

謝謝。

回答

4
ps -wef | awk 'NR>1 {printf("%s ", $2)}END{printf("\n")' 

我希望這有助於

+0

謝謝!它很棒! –

3

您還可以使用-o選項僅在第一時間拿到的地方PID列:

ps --no-headers -eo pid 

我放棄了-w-f,因爲你會看到他們對輸出的貢獻,並且用--no-headers刪除了標題。

1

這可能會爲你工作(GNU SED):

sed '1d;s/\S*\s*\(\S*\).*/\1/' file 

更靈活的方法:

sed '1d;s/\(\(\S*\)\s*\)\{2\}.*/\2/' file 

因此,如果你想TIME柱:

sed '1d;s/\(\(\S*\)\s*\)\{7\}.*/\2/' file