0

How to print column free using awk or sed command?

Depends on vmstat version column, free may change from column number 5 to something else.

bash-3.00$ vmstat 1 1|tail -2
r b w swap free re mf pi po fr de sr s0 s1 s3 -- in sy cs us sy id
0 0 0 38018936 24998752 122 182 33 81 53 0 0 5 7 -0 0 4113 8416 4527 1 0 99
bash-3.00$ vmstat 1 1|tail -2|awk '{print $5}'
free
24998752
2

You would, for the first line of input, loop over all fields to find the field that is equal to the string free. You would remember this field number and then use it to output only that field:

awk 'NR==1 { for (i=1;i<=NF;++i) if ($i=="free") { n=i; break }} { print $n }'

The NR==1 makes the following block only execute for the very first line, i.e. the header line. The variable n will hold the field number of the free field.

Testing it on the vmstat 1 1|tail -2 data that you present (read from the file called file here):

$ awk 'NR==1 { for (i=1;i<=NF;++i) if ($i=="free") { n=i; break }} { print $n }' file
free
24998752

My original answer used FNR rather than NR. Unfortunately, the awk on SunOS does not have the FNR variable, but in this case the NR variable may be use without changing anything else.

Also note that the output of vmstat on some systems show the free column as fre (OpenBSD, for example). This means that you may want to use $i ~ "^fre" instead of $i == "free".

  • bash-3.00$ vmstat 1 1|tail -2|awk 'FNR==1 { for (i=1;i<=NF;++i) if ($i=="free") { n=i; break }} { print $n }' r b w swap free re mf pi po fr de sr s0 s1 s3 -- in sy cs us sy id 0 0 0 38018928 24998728 122 182 33 81 53 0 0 5 7 -0 0 4113 8416 4527 1 0 99 --> not working – Anavarathan Apr 12 at 12:58
  • @RoVo please share the output & full command – Anavarathan Apr 12 at 13:02
  • works well in lunix. but not working in SunOS – Anavarathan Apr 12 at 13:05
  • 1
    @Anavarathan On SunOS, change FNR==1 to NR==1. With this change, it would still work on Linux. – Kusalananda Apr 12 at 13:06
  • working thank you for the great support mate. – Anavarathan Apr 12 at 13:07
1

Use csvcut tool from csvkit:

vmstat 1 1 | tail -2 | tr -s ' ' | csvcut -d ' ' -c free

or

vmstat 1 1 | tail -2 | csvcut -d ' ' -S -c free
  • You need to "squeeze" the space to make this work, add tr -s ' ' after tail – Thor Apr 12 at 12:56
0

I have done by below method

for ((i=1;i<=10;i++)); do free -mh| awk -v i="$i" '$i=="free"{x=NR+2}(NR<=x) {print $i}'; done


output
for ((i=1;i<=10;i++)); do free -mh| awk -v i="$i" '$i=="free"{x=NR+2}(NR<=x) {print $i}'; done
free
859M
0B
praveen@praveen:~$ 
  • free command not available in sun os – Anavarathan Apr 15 at 5:56
-1

This: vmstat 1 1 | sed -e 's/ +/ /g' | tail -2 | cut -d' ' -f5

Or this: vmstat 1 1 | tail -2 | tr -s ' ' | cut -d' ' -f5

  • This does not solve the problem: free may change from column number 5 to something else. – pLumo Apr 12 at 13:09
  • The issue is that on different systems, the wanted column shifts from being column 5 to being something else. – Kusalananda Apr 12 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.