LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2013, 05:51 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
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?
 
Old 11-07-2013, 07:21 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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"
 
Old 11-07-2013, 07:55 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
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'

Last edited by syg00; 11-07-2013 at 07:56 PM. Reason: typos
 
Old 11-14-2013, 03:30 PM   #4
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Great..thank to all, I'll give all the suggestions a try!
 
  


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
[SOLVED] grep -o and strange character di80 Linux - Newbie 4 10-16-2012 07:19 AM
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 09:41 PM
[SOLVED] vim: to input a single character and continue in command mode: what key? stf92 Linux - Newbie 12 06-24-2011 09:45 PM
[SOLVED] How to grep, cut, cat in a single command? dwarf007 Programming 6 04-27-2010 03:36 AM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 05:16 AM


All times are GMT -5. The time now is 09:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration