LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-06-2006, 01:48 AM   #1
3saul
Member
 
Registered: Aug 2004
Location: Brisbane, Australia
Distribution: Ubuntu/Fedora
Posts: 86

Rep: Reputation: 15
Yet another SED Question...but more difficult...


I have a file that contains blocks of text such as:

<stuff>
<number="false">
<name>jonno</name>
<filename>/home/john/file</filename>
<options>male</options>
</stuff>

However this file contains many of these with only minor differences...eg:


<stuff>
<number="false">
<name>jonno</name>
<filename>/home/john/file</filename>
<options>male</options>
</stuff>

<stuff>
<number="true">
<name>mary</name>
<filename>/home/mary/file</filename>
<options>male</options>
</stuff>

I'm wanting to selectively remove one of these blocks of text from the file and save it permanently. Can this be done with SED?
 
Old 01-06-2006, 01:50 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
No. I don't think so. sed is a line editor, it reads on a line-per-line basis.
You're better of with either awk or a Perl script, in my humble opinion.
 
Old 01-06-2006, 01:58 AM   #3
3saul
Member
 
Registered: Aug 2004
Location: Brisbane, Australia
Distribution: Ubuntu/Fedora
Posts: 86

Original Poster
Rep: Reputation: 15
I don't really mind how it gets done (perl/awk/grep/sed) - any tips on how to do it in perl/awk?
 
Old 01-06-2006, 03:15 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
In Perl, your algo could be something like:
-open file for reading
-open up 2 files for writing: 1 to save the blocks you want to keep, 1 for the others
-allocate an array that you're going to use as a buffer
-loop over all lines in the file you're reading
-if line =~ <stuff>, you're at the beginning of a new block.
In such case, you need to check if you want to keep the block you've just read and
print it to the right output file.
-else, you need to simply append the line you've read to your buffer.
-after the loop, close all files

Once the script has done it's work, check out it's two output files. If the file that contains
the blocks you want to keep seems OK to you, mv it over the original file.
 
  


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
Why it is so difficult do insert a new question Paul7 LQ Suggestions & Feedback 10 08-15-2005 09:42 AM
sed question provkitir Programming 4 05-03-2005 09:41 AM
sed question virendratp Programming 3 09-10-2004 03:21 AM
sed question dolvmin Linux - Software 4 03-09-2004 09:27 AM
sed question 360 Programming 2 03-15-2002 09:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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