Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK Print Second Column of Last Line

Tags:

shell

awk

I'm trying to use AWK to post the second row of last line of this command (the total disk space):

df --total

The command I'm using is:

df --total | awk 'FNR == NF {print $2}'

But it does not get it right. Is there another way to do it?

like image 776
user2248259 Avatar asked Feb 23 '14 10:02

user2248259


3 Answers

You're using the awk variable NF which is Number of Fields. You might have meant NR, Number of Rows, but it's easier to just use END:

df --total | awk 'END {print $2}'
like image 190
John Zwinck Avatar answered Sep 18 '22 20:09

John Zwinck


You can use tail first then use awk:

df --total | tail -1 | awk '{print $2}'
like image 37
vahid abdi Avatar answered Sep 20 '22 20:09

vahid abdi


One way to do it is with a tail/awk combination, the former to get just the last line, the latter print the second column:

df --total | tail -1l | awk '{print $2}'

A pure-awk solution is to simply store the second column of every line and print it out at the end:

df --total | awk '{store = $2} END {print store}'

Or, since the final columns are maintained in the END block from the last line, simply:

df --total | awk 'END {print $2}'
like image 23
paxdiablo Avatar answered Sep 20 '22 20:09

paxdiablo