Tuesday, February 1, 2022

[SOLVED] free -g with sed -n plus nth character

Issue

I am trying to create a script to display just one of the numbers from the free command. In my case, the number I want is on the 2nd line of the free -g output and at character slot 43. The original output of free -g is:

              total       used       free     shared buff/cache  available
Mem:              3          0          2          0          0          2     
Swap:             3          0          3          

So far I have gotten the 2nd line to display with:

free -g | sed -n 2p
Mem:              3          0          2          0          0          2

I need just the 43rd character of that line or the 4th column? I have looked into printf and cut but get a grody syntax error.


Solution

If a line contains Mem: print from this line column 4:

free -g | awk '/Mem:/ {print $4}'

Output:

2


Answered By - Cyrus
Answer Checked By - Marilyn (WPSolving Volunteer)