Home > Software engineering >  How to get only second line using awk
How to get only second line using awk

Time:11-02

Hello stack overflow community.

Wondering if you can help me with this.

I want to get the available storage space on a computer system and export it to a variable.

I know the command below will give me the available storage on a system. export HOME=cd;pwd df -H --output=avail ${HOME}

But it gives me a header of Avail

Avail
891G

I only want the 891G to be exported as a string into a variable.

I tried this command below but it didn't work

df -H --output=avail ${HOME} | awk -F"\n" '{print$1}'

Any thoughts?

CodePudding user response:

If you know for a fact that you'll always be looking for the value in the 2nd line:

$ df -H --output=avail ${HOME} | awk 'NR==2'
   39G

To remove the leading space you have a few options; one small addition to the current awk idea:

$ df -H --output=avail ${HOME} | awk 'NR==2 {print $1}'
39G

Then storing in a variable:

$ size=$(df -H --output=avail ${HOME} | awk 'NR==2 {print $1}')
$ typeset -p size
declare -- size="39G"
  • Related