help with awk

Hello,
below is the df output from Solaris.

test@test# df -h .
Filesystem size used avail capacity Mounted on
rpool/app/oracle 20G 14G 5.7G 72% /app/oracle

I need to get just the capacity --> 72
So far:

df -h /app/oracle |grep -v Filesystem | awk '{print $(NF-1)}'

Output ---> 72%

a)How do I remove the first line, instead of grep -v Filesystem?

b)How do I remove the % sign?

Thank you in advance.

try this, and here's a reference to some good awk one-liners, http://www.pement.org/awk/awk1line.txt

df -h /app/oracle | awk '!/Filesystem/{ sub(/%/, ""); print $(NF-1); }'
1 Like

Use search, you will get lots of threads asking the same.

awk '/%/ {print int($(NF-1))}'

Thank you.

How do I do it instead of

'!/Filesystem/
to specify the first line (NR, FNR?), just for future usage ?

that would be an if statement comparing NR with the line number to ignore. something basically like this.

awk '{if (NR != 1) print int($(NF-1))}'
1 Like

The same thing :

df -h | awk 'NR>1 { print int($(NF-1)) }'

Jean-Pierre.

df -h /app/oracle |awk '{if (NR != 1) print $(NF-1)}'

Is working properly and outputs 72%

However I am having trouble with the sub command.
I cannot remove the % symbol.

Please help

There are many ways you can do it, e.g. use sub as in unxscorob's first answer, or just pipe it into sed:

df -h /app/oracle |awk '{if (NR != 1) print $(NF-1)}' | sed 's/%//'

df -h /app/oracle | awk '!/Filesystem/{ sub(/%/, ""); print $(NF-1); }'
awk: syntax error near line 1
awk: illegal statement near line 1

add the int() function as in anchal_khare's reply

df -h /app/oracle |awk '{if (NR != 1) print int($(NF-1))}'

Try

df -h /app/oracle | awk '{if (NR != 1) { sub (/%/,""); print $(NF-1)}}'

Still not working

df -h /app/oracle | awk '{if (NR != 1) { sub (/%/,""); print $(NF-1)}}'
awk: syntax error near line 1
awk: illegal statement near line 1

Try nawk instead of awk.

EDIT: (or GNU awk, which apparently is what I'm using)

1 Like

OK so sub is not available in awk :slight_smile:

However now it workes but gives me part of the header"

Mounted
60

PS: Thanks for your help and sorry for the trouble

---------- Post updated at 03:48 PM ---------- Previous update was at 03:45 PM ----------

Solved ! Thank you all