LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-19-2008, 12:31 AM   #1
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Rep: Reputation: 0
how to grep for only the values of a specific field


hi let say i have a config file that contain the followng:
MEM_PERCENT=(1 2 3 4)
NUM_OPER=(5 6 7 8)

how do i grep for only 1 2 3 4 and assign to a variable in my script?

i tried

content=$(grep -E "MEM_PERCENT" config)

but its return the whole line.



thanks
 
Old 11-19-2008, 01:03 AM   #2
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Rep: Reputation: 46
prob not the neatest way but this is how I'd do it:
Code:
content=`cat config | grep "MEM_PERCENT" | sed 's/.*(//g' | sed 's/).*//g'`
as I said it's not the simplest way to write it out but it is the way I understand it best.

Last edited by johnson_steve; 11-19-2008 at 01:05 AM.
 
Old 11-19-2008, 01:21 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Code:
content=$(awk -F"[\(\)]" '/MEM_PERCENT/ {print $2}' config)
and the cat in the grep/sed approach is superfluous.
Code:
content=`grep "MEM_PERCENT" config | sed 's/.*(//g' | sed 's/).*//g'`

Last edited by Tinkster; 11-19-2008 at 01:27 AM.
 
Old 11-19-2008, 08:20 AM   #4
johnson_steve
Senior Member
 
Registered: Apr 2005
Location: BrewCity, USA (Milwaukee, WI)
Distribution: Xubuntu 9.10, Gentoo 2.6.27 (AMD64), Darwin 9.0.0 (arm)
Posts: 1,152

Rep: Reputation: 46
I said it was sloppy.
 
  


Reply


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
grep for specific phrases Harry Seldon Linux - Newbie 5 05-27-2008 07:58 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM
Replacing text in a specific field Seventh_Warrior Programming 5 04-11-2005 12:04 PM
storing multiple values within one field in mysql antken Programming 8 12-15-2002 10:08 PM


All times are GMT -5. The time now is 08:09 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration