LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04:51 PM   #1
Drigo
Member
 
Registered: May 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, 06:21 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 06:55 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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 06:56 PM. Reason: typos
 
Old 11-14-2013, 02:30 PM   #4
Drigo
Member
 
Registered: May 2009
Posts: 111

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:29 AM.

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
Open Source Consulting | Domain Registration