LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-17-2006, 03:36 PM   #1
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
help with sed


Here is what I'm trying to do.

I have file with tags simular to the one below
Code:
<tre>hello</tre><lkj>world</lkj><sdf>tux</sdf><tre>cool</tre>
<lkj>time</lkj><sdf>linux</sdf>
I want to write a script that will collect the info between the 'tre' and the 'sdf' tags and put them in a text files like below:
Code:
hello tux
cool linux
I've tryed to use 'sed' but I'm new to this command and can't get it right or 'sed' is not the right command to use.
 
Old 09-17-2006, 04:34 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
As sed proceeds linewise, you should tell us how many of these tags appear on a single line and specify which ones should appear on each line of the output. If there are no such rules, it's probably better to use Perl and process the entire file all at once.
 
Old 09-17-2006, 04:41 PM   #3
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Original Poster
Rep: Reputation: 16
It has no line order. Everything is bunched together. And, It's a few pages long. How would I go about doing it in Perl, if it is possible?
 
Old 09-17-2006, 06:56 PM   #4
kryptonite0110
Member
 
Registered: Mar 2005
Posts: 59

Rep: Reputation: 15
replace > with >\n, now you have:

<tre>
hello</tre>
<lkj>
world</lkj>
<sdf>
tux</sdf>
<tre>
cool</tre>
<lkj>
time</lkj>
<sdf>
linux</sdf>

then delete ^<*>$ (i don't know if ^ is the beggining or end, so it may be $<*>^) lines
then grep tre, sdf
then s/</.*>//
:edit

it's possible to optimize this in a number of ways, i'm not going to do that for you...

:edit2

i just realized, that for that to work, you'd need to put everyhing on one line (or at least the </tag> on the same line as the contents of said tag...) s/\n// may work...

Last edited by kryptonite0110; 09-17-2006 at 06:59 PM.
 
Old 09-17-2006, 07:05 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you could do it with regular expressions in perl, or python
an example only, Python:
Code:
import re
s = "<tre>hello</tre><lkj>world</lkj><sdf>tux</sdf><tre>cool</tre><lkj>time</lkj><sdf>linux</sdf>"
trepat = re.compile(r"<tre>(.*?)</tre>",re.M)
sdfpat = trepat = re.compile(r"<sdf>(.*?)</sdf>",re.M)
tre = trepat.findall(s)
sdf = sdfpat.findall(s)
zip(tre,sdf) ## "transpose" function here.
output:
Code:
>>> zip(tre,sdf)
[('hello', 'tux'), ('cool', 'linux')]
can be converted to perl easiliy..
 
Old 09-17-2006, 07:27 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Here's a Perl one-liner. It will begin a new line after each sdf-tag:
Code:
ada@barnabas:~/tmp> echo -e "<tre>hello</tre><lkj>world</lkj><sdf>tux</sdf><tre>cool</tre>\n<lkj>time</lkj><sdf>linux</sdf>" |
> perl -ne 'while ( m/<(tre|sdf)>(.*?)</g) {print "$+ "; print "\n" if ( "$1" eq "sdf" );}'
hello tux
cool linux
 
Old 09-18-2006, 09:18 AM   #7
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Original Poster
Rep: Reputation: 16
Smile Thanks

Quote:
Originally Posted by spirit receiver
Here's a Perl one-liner. It will begin a new line after each sdf-tag:
Code:
ada@barnabas:~/tmp> echo -e "<tre>hello</tre><lkj>world</lkj><sdf>tux</sdf><tre>cool</tre>\n<lkj>time</lkj><sdf>linux</sdf>" |
> perl -ne 'while ( m/<(tre|sdf)>(.*?)</g) {print "$+ "; print "\n" if ( "$1" eq "sdf" );}'
hello tux
cool linux
Thank you everyone

This Perl one-liner did the job great.

Thanks once again.
 
  


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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
using sed ilnli Linux - General 3 07-01-2005 03:59 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

All times are GMT -5. The time now is 05:26 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