LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash cut command to get df percentage (https://www.linuxquestions.org/questions/programming-9/bash-cut-command-to-get-df-percentage-286213/)

nutthick 02-04-2005 11:19 AM

bash cut command to get df percentage
 
I'm trying to get bash to return the percentage free disk space as an integer using df and cut, but it doesn't seem to work. I'm trying

df /home | cut -f5

but it just echos as if I had typed df /home

Anyone know what is going on? Also, how can I just extract the value from the second line so I don't end up with

Use%
50%

Thanks

ilikejam 02-04-2005 11:29 AM

Try:

df /home | awk '{ print $5 }' | tail -n 1

Dave

nutthick 02-04-2005 11:47 AM

Thanks ilikejam, I've never heard of awk. One last question you might be able to help with.

Is there a string command in bash to allow me to strip out the piece of the string that I need and assign it to a variable? In this case it would be removing the % but it's a useful thing to know and my books don't seem to mention it.

Thanks so far

ilikejam 02-04-2005 12:11 PM

There's several ways.

If you know the character (or string) you want rid of, then you can use 'sed'

e.g. df /home | awk '{ print $5 }' | tail -n 1 | sed 's/%//'
The s/%// replaces % with nothing. s/%/hello/ would replace the '%' with 'hello'

If you know where in the line the character is, then you can use 'colrm'

e.g. df /home | awk '{ print $5 }' | tail -n 1 | colrm 1 1
The two numbers after colrm are the 'column range'. Here we want to remove the first column (the first character), so we use column 1 to column 1.

If you want that in a variable, then you can use the `...` inline substitution

e.g. VAR1=`df /home | awk '{ print $5 }' | tail -n 1 | sed 's/%//'`
Any command between ` quotes gets replaced by whatever the command would normally output.

Dave

nutthick 02-04-2005 12:17 PM

Thanks Dave I was looking at substr, but your way is a lot simpler. The whole string manipulation thing seems a bit messy in bash.

Thanks for your help

nutthick

95se 02-04-2005 12:56 PM

you could also use the cut command
df | awk '{print $5}' | cut -d '%' -f 1

Brain Drop 02-05-2005 02:36 PM

Another way, without awk or sed:

df /home/|tr -s ' ' |cut -d' ' -f5|tail -n1

Your original question: cut has tab as default delimiter, works if you change to space, and squeeze the spaces.

rommelsharma 02-17-2005 06:09 AM

Also try out the following combination:

df -k /home | awk '{ print $5 }'|grep %|cut -d% -f 1

It removes the % size and lists the size of the directory specified.

On solaris 5.9, when I run

df /home | awk '{ print $5 }' | tail -n 1

It complains:

usage: tail [+/-[n][lbc][f]] [file]
tail [+/-[n][l][r|f]] [file]

Any inputs?

Rommel Sharma.

sigma13 12-16-2010 11:02 AM

Parse df for Solaris
 
5 years later...
For Solaris 5.10 try this:
df /home | awk '{ print $5 }' | tail -1

tail -1 also prints the last line, which works both on linux and solaris boxes..
greets


All times are GMT -5. The time now is 02:57 PM.