LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-25-2005, 06:09 AM   #1
colabus
Member
 
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.
Code:
/dev/hdb             117217216  93883264  23333952  81% /data
I have gotten it down to:
Code:
$ 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
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
> STRING='/dev/hdb             117217216  93883264  23333952  81% /data'
> echo ${STRING} | cut -f 5 -d" " | sed 's/%//'
81
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
hussar
Member
 
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
59

(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
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Here are a couple of different approaches:-
Code:
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...
 
  


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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:03 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