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? |
firstly avoid useless use of cat
http://partmaps.org/era/unix/award.html#uucaletter Code:
grep "DX Group" XXX.xml a few ways, awk is useful here Code:
awk -F "[<>]" '/DX Group/{print $3}' XXX.xml and print "field 3" of lines that contain "DX Group" |
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' |
Great..thank to all, I'll give all the suggestions a try!
|
All times are GMT -5. The time now is 12:40 PM. |