LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2012, 05:30 PM   #1
hanae
Member
 
Registered: May 2012
Posts: 33

Rep: Reputation: Disabled
Smile grep command related Question


Hello,
I am new in LinuxQuestions and I have really loved it so far.
Well, I want to extract specific information that it is between one specific tags. This is a simple input:
<TOKEN id="articleA" start_char="3" end_char="6">word1</TOKEN>

I want to have this output:

word1 articleA 3

I need to this for multiple files that have this patter <TOKEN.....</TOKN> repeated over all the lines.

Can you please help me with it?

Thank you
 
Old 05-16-2012, 06:05 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Sure, there will be quicker ways of doing it, but here's my version:

Code:
sed 's/\(.[^"]*\)"\(.[^"]*\)"\(.[^"]*\)"\(.[^"]*\)"\(.[^>]*\)>\(.[^<]*\).*/\6 \2 \4/' file
 
1 members found this post helpful.
Old 05-16-2012, 07:55 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,787

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by hanae View Post
This is a simple input:
<TOKEN id="articleA" start_char="3" end_char="6">word1</TOKEN>

I want to have this output:

word1 articleA 3
The input looks like XML, if it is valid XML you should use an XML based tool, eg XMLStarlet:
Code:
xmlstarlet sel -T -t -m //TOKEN -v . -o ' ' -v @id -o ' ' -v @start_char --nl input.xml
 
1 members found this post helpful.
Old 05-17-2012, 04:56 AM   #4
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you very much sycamorex for your help. Indeed this command does exactly what I want

ntubski, Thanks alot, too, but I have never used xmlStarlet, I tried to download from sourceforge, But I couldn't figure out how to use it??
 
Old 05-17-2012, 11:45 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,787

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by hanae View Post
ntubski, Thanks alot, too, but I have never used xmlStarlet, I tried to download from sourceforge, But I couldn't figure out how to use it??
If you're on Windows (I assume from the icon on your posts) there is a precompiled binary. Small difference in usage: the executable is named xml.exe, not xmlstarlet.exe, and you need to use " " instead of ' ', because cmd.exe doesn't recognize single quotes as quoting characters.
 
1 members found this post helpful.
Old 05-18-2012, 04:17 AM   #6
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks a lot ntubski.
 
  


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
having df command related question aish11 Programming 2 02-07-2012 07:25 AM
Question related to export command ggray Linux - Newbie 2 04-05-2011 08:58 AM
[SOLVED] I am requesting help related to the grep command srajeshkumar Linux - Newbie 5 12-13-2010 03:28 AM
grep related question... robertjinx Linux - Server 2 11-19-2010 05:52 PM
Command syntax question for grep kaplan71 Linux - Software 4 07-16-2008 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 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
Open Source Consulting | Domain Registration