[英]Awk multiple columns of top
在RedHat上,使用以下命令(十個cpu用戶):
top -n 1 -b -c|head -17|tail -11
我只需要PID,USER,%CPU,%MEM,TIME +和COMMAND列
理想情況下,我希望在第一列之間使用制表符,然后使用空格分隔符正常打印COMMAND列。
因此,類似於:
top -n 1 -b -c|head -17|tail -11|awk '{print $1"\t"$2"\t"$9"\t"$10"\t"$11"\t"} {for (i=12;i<=NF;i++) printf("%s ",$i)} {print ""}'
但是,兩個打印語句之間會出現換行符。
示例輸出:
PID USER %CPU %MEM TIME+ COMMAND
15968 root 17.8 0.0 0:00.11 /usr/local/bin/pmun -p -h vm21fc root
15962 igsg093 16.2 0.1 0:00.16 top -n 1 -b -c
15966 idig056 6.5 0.1 0:00.04 /usr/bin/perl -w /scripts/script.pl arg
15969 root 6.5 0.1 0:00.04 pmasterd -ars
1 root 0.0 0.0 0:03.37 init [3]
2 root 0.0 0.0 3:38.62 [migration/0]
3 root 0.0 0.0 0:29.41 [ksoftirqd/0]
4 root 0.0 0.0 0:00.00 [watchdog/0]
5 root 0.0 0.0 0:03.70 [events/0]
6 root 0.0 0.0 0:00.00 [khelper]
任何援助將不勝感激。 謝謝!
。
附加幫助:如果有可能將第二個打印語句(對於COMMAND)的字符輸出剪切為一定長度,這也可能會有所幫助。
只需更改此部分: {printf "%s", $1"\\t"$2"\\t"$9"\\t"$10"\\t"$11"\\t"}
您也可以跳過頭和尾:
top -n 1 -b -c| awk 'NR >= 7 && NR <= 17 {printf "%s", $1"\t"$2"\t"$9"\t"$10"\t"$11"\t"; for (i=12;i<=NF;i++) printf("%s ",$i); print ""}'
您可以使用OFS
(輸出字段分隔符)變量,以便print
為您處理選項卡:
top -n 1 -b -c| awk -vOFS="\t" 'NR>6 && NR<18 {s=""; for (i=12;i<=NF;++i) s=s FS $i; print $1,$2,$9,$10,$11,s}'
逗號分隔的參數將在它們之間打印有OFS。
如果要限制s
的長度,可以在print語句中對s
進行substr
:
print $1,...,substr(s,0,N)
其中N
是命令的最大長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.