Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get only second line using awk

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading