LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2005, 11:19 AM   #1
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Rep: Reputation: 30
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
 
Old 02-04-2005, 11:29 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Try:

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

Dave
 
Old 02-04-2005, 11:47 AM   #3
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
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
 
Old 02-04-2005, 12:11 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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

Last edited by ilikejam; 02-04-2005 at 12:13 PM.
 
Old 02-04-2005, 12:17 PM   #5
nutthick
Member
 
Registered: Jun 2004
Distribution: Slack
Posts: 214

Original Poster
Rep: Reputation: 30
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
 
Old 02-04-2005, 12:56 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
you could also use the cut command
df | awk '{print $5}' | cut -d '%' -f 1
 
Old 02-05-2005, 02:36 PM   #7
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 35
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.

Last edited by Brain Drop; 02-05-2005 at 02:39 PM.
 
Old 02-17-2005, 06:09 AM   #8
rommelsharma
LQ Newbie
 
Registered: Feb 2005
Location: India
Posts: 1

Rep: Reputation: 0
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.
 
Old 12-16-2010, 11:02 AM   #9
sigma13
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple bash question about cut perfect_circle Programming 2 05-17-2005 11:25 AM
finger and cut command Craig Bowes Linux - Newbie 3 10-09-2004 12:49 PM
Cut Command Help!! fooforon Programming 2 02-05-2004 10:09 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM
Easy: Help with Cut command Krash_io Programming 4 07-17-2002 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration