2013-07-04 68 views
1

條件我怎樣才能將這個命令加盟AWK

top -b -n 5 -d.2 | grep "Cpu" | awk 'NR==3{ print($2)}' 

到只有awk命令(加盟的grep和awk成一個)?

我已經試過,但沒有成功:

top -b -n 5 -d.2 | awk '{if($1 == "Cpu(s):" && NR==3){ print($2)} }' 

top -b -n 5 -d.2 | awk '{$1 ~ /Cpu/ && (NR==3) { print($2)}}' 

回答

1
top -b -n 5 -d.2 | awk '/Cpu/ { if (++cnt==3) print $2 }' 
2
awk '/Cpu/ {x++; if(x==3) { print $2}}' 

注意:您可以短路添加exit