Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-07-2013, 05:51 PM   #1
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"?>
<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
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

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

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
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,949

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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
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
Registered: Jun 2009
Posts: 111

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration