LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to grep for only the values of a specific field (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-grep-for-only-the-values-of-a-specific-field-684490/)

hchoonbeng 11-19-2008 12:31 AM

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

johnson_steve 11-19-2008 01:03 AM

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.

Tinkster 11-19-2008 01:21 AM

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

johnson_steve 11-19-2008 08:20 AM

I said it was sloppy.


All times are GMT -5. The time now is 09:00 PM.