Patryk
Patryk

Reputation: 24092

Join conditions in awk

How can I join this command

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

into only awk command (joining grep and awk into one) ?

I have tried this but with no success:

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

or

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

Upvotes: 1

Views: 136

Answers (2)

Ed Morton
Ed Morton

Reputation: 203615

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

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96266

awk '/Cpu/ {x++; if(x==3) { print $2}}'

Note: you can add exit for short-circuiting.

Upvotes: 2

Related Questions