Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-25-2005, 06:09 AM   #1
Registered: Mar 2004
Distribution: Debian Sarge, FC4
Posts: 100

Rep: Reputation: 15
Simple bash script help, grabbing part of a string

Hi guys,

Can I get some help?

I want to grab the "81" value in my string.
/dev/hdb             117217216  93883264  23333952  81% /data
I have gotten it down to:
$ df|grep /dev/hdb|cut -d"%" -f 1
/dev/hdb             117217216  93883264  23333952  81
I need to grab the first string from the right delimited by " ", any ideas anyone?

ANy help or advice would be great
Old 04-25-2005, 06:27 AM   #2
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657
> STRING='/dev/hdb             117217216  93883264  23333952  81% /data'
> echo ${STRING} | cut -f 5 -d" " | sed 's/%//'
I used an echo command as the source of the string, but you could probably use a file instead.
The cut command selects the fifth field, while the sed command removes the percent sign.

You might also use an awk filter instead. It is better than sed at selecting fields from the input.
Old 04-25-2005, 07:49 AM   #3
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
There is also a way to do this with bash's substring removal tools as described in the Advanced Bash Scripting Guide, section 9.2. For example:

bash-3.00$ STRING=`df | grep /dev/hda5`
bash-3.00$ STRING=`echo $STRING | cut -f 5 -d " "`
bash-3.00$ STRING=`echo ${STRING%\%}`
bash-3.00$ echo $STRING

(I used my own /dev/hda5 for this example. That's why the result is not 81.)
Old 04-25-2005, 09:42 AM   #4
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Here are a couple of different approaches:-
df | grep /hdb | awk -F " " '{print $5}' | tr -d "%"
df | grep /hdb | sed -e 's/.*[[:digit:]] \+//' -e 's/%.*//'
If it matters, the sed variant will save you a millisecond or so...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM

All times are GMT -5. The time now is 07:41 AM.

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