how to extract the remaining memory from free using awk?

1.8k views Asked by At

I need to extract the available memory from the output free and I thought I would use awk and came up with something like free | awk '{print $4}'. Which gives me an output like:

$ free | awk '{print $4}'
shared
365296
1273812
3931364

mind, the title shared isn't the title of these numbers, the numbers are from free (/usr/bin/free has no title in the first column, thus the numbers for free appear in the 4th column where the title reported is from the 5th). However, having this, how do I only return the second line? I'm not interested in the rest for now.

2

There are 2 answers

1
stdcerr On

Just figured it out myself, it can be accomplished using:

$ free | awk '{print $4}'| head -2| tail -1
5
iamauser On
free | awk 'NR==2 {print $4}'

NR, is the ROW/Line number