LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Use grep to grab a value in between character with a single command :/ (https://www.linuxquestions.org/questions/linux-newbie-8/use-grep-to-grab-a-value-in-between-character-with-a-single-command-4175483784/)

Drigo 11-07-2013 04:51 PM

Use grep to grab a value in between character with a single command :/
 
I have a file (xml) with the following content:

$cat XXX.xml

<?xml version="1.0" encoding="UTF-8"?>
.
.
.
<subject>
<subjectIdentifier>127_S_5218</subjectIdentifier>
<researchGroup>Patient</researchGroup>
<subjectSex>F</subjectSex>
<subjectInfo item="DX Group">SMC</subjectInfo>
.
.
.



*I want to grep the value specifying the Dx group. In this case SMC. This value will change between different xml files...

So I tried:
$cat XXX.xml | grep "DX Group"
<subjectInfo item="DX Group">SMC</subjectInfo>

But it grep the entire line rather that just the characters i need (e.g. XMC).
Any help?

Firerat 11-07-2013 06:21 PM

firstly avoid useless use of cat
http://partmaps.org/era/unix/award.html#uucaletter

Code:

grep "DX Group" XXX.xml
now to achieve what you want
a few ways,
awk is useful here

Code:

awk -F "[<>]" '/DX Group/{print $3}' XXX.xml
there we use both < and > as field separators

and print "field 3" of lines that contain "DX Group"

syg00 11-07-2013 06:55 PM

grep is for finding text - even has the option to only print what was found (rather than the entire line). Doesn't really fit for this - sed is another option
Code:

sed -nr 's:.*DX Group">(.{3}).*:\1:p'

Drigo 11-14-2013 02:30 PM

Great..thank to all, I'll give all the suggestions a try!


All times are GMT -5. The time now is 12:40 PM.