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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 03-13-2009, 09:14 PM   #1
djmm
LQ Newbie
 
Registered: Sep 2004
Posts: 4
Thanked: 0
sed multi-line search/replace woes


[Log in to get rid of this advertisement]
Any idea why this isn't inserting </UL>'s across the whole file?
What'd I miss?

Code:
# foobar3 is what you see below minus the </UL> (all spaces no tabs)
$ sed -e 'N;s,\(^    \* .*\n\) \*,\1</UL>\n *,g'  foobar3

 * heading 1
    * subheading 1
    * subheading 2
 * heading 2
 * heading 3
    * subheading 1
    * subheading 2
    * subheading 3
</UL>
 * heading 4
 * heading 5
 * heading 6
 * heading 7
    * subheading 1
    * subheading 2
    * subheading 3
* heading 8
djmm is offline  
Tag This Post , ,
Reply With Quote
Old 03-13-2009, 09:21 PM   #2
syg00
Guru
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 6,913
Thanked: 166
ignore me - irrelevant post.

Last edited by syg00; 03-13-2009 at 09:26 PM..
syg00 is offline     Reply With Quote


Old 03-13-2009, 09:30 PM   #3
djmm
LQ Newbie
 
Registered: Sep 2004
Posts: 4
Thanked: 0

Original Poster
Quote:
Originally Posted by syg00 View Post
sed doesn't pass the newlines - you get to see the "stripped" (chomp'd in perl parlance) line.
awk or perl might be a better bet.
Sorry, I'm not following. N pulls in the next line, so the buffer always has 2 lines it in with the \n in it. Many references talk about using this method to read in 2 lines with the \n embeded (not stripped).

My question is, why is this working for ONE of the conditions met in the file and not the others. The middle one even, not even the first or last. Very strange.
djmm is offline     Reply With Quote


Old 03-13-2009, 10:21 PM   #4
syg00
Guru
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 6,913
Thanked: 166
Yeah I had deleted that, but you'd already started replying apparently ... sorry.

Thinking about it some more, I think you're failing because you appear to be expecting each buffer will have every line followed by the next. What you actually get is every second line plus the next.
i.e. first data you get to work on is "line1\nline2", then you get "line3\nline4" ... (note you don't get "line2\nline3")

Add some dummy lines to your input, and you'll see what I mean - even using addressing won't get around this. Seems my suggestion of awk or perl still stands - but for a (hopefully) valid reason this time.
syg00 is offline     Reply With Quote


Old 03-13-2009, 10:22 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 115
show in more details how your input file looks like, how you want your final output to be like. you do not need complex regular expressions to do what you want.
ghostdog74 is offline     Reply With Quote


Old 03-16-2009, 05:25 PM   #6
djmm
LQ Newbie
 
Registered: Sep 2004
Posts: 4
Thanked: 0

Original Poster
Quote:
Originally Posted by ghostdog74 View Post
show in more details how your input file looks like, how you want your final output to be like. you do not need complex regular expressions to do what you want.
OK, here's the starting text:

Code:
 * heading 1
    * subheading 1
    * subheading 2
 * heading 2
 * heading 3
    * subheading 1
    * subheading 2
    * subheading 3
 * heading 4
 * heading 5
 * heading 6
 * heading 7
    * subheading 1
    * subheading 2
    * subheading 3
* heading 8
    * subheading 1
Here's how I want it to look:

Code:
 * heading 1
    * subheading 1
    * subheading 2
</UL>
 * heading 2
 * heading 3
    * subheading 1
    * subheading 2
    * subheading 3
</UL>
 * heading 4
 * heading 5
 * heading 6
 * heading 7
    * subheading 1
    * subheading 2
    * subheading 3
</UL>
* heading 8
    * subheading 1
</UL>
I'm actually wrapping all the subheading blocks in <UL> and </UL>, but I already have the <UL> part working and trying to use the most simple example here, but will mention that here for the sake of context of this exercise.

Thanks for you help!
djmm is offline     Reply With Quote


Old 03-16-2009, 06:23 PM   #7
djmm
LQ Newbie
 
Registered: Sep 2004
Posts: 4
Thanked: 0

Original Poster
I got this working with awk, though I'd be curious to know if there's a way to do it with sed. syg00 pointed to the problem with the "N;" solution.

Code:
awk 'BEGIN {mark=0}
     /^    \*/ {mark=1}
     /^ \*/ {if (mark) print "</UL>";mark=0}
     {print}'
djmm is offline     Reply With Quote


Old 03-17-2009, 06:10 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 171
Thanked: 32
This uses GNU sed to insert both tags; remove 'i\
<UL>' if you want to leave out the <UL> tags.
Code:
sed '/^    \*/{i\
<UL>
:a
$! {n;/^    \*/ba;i\
</UL>
}
$ {/^    \*/a\
</UL>
};}'

 * heading 1
<UL>
    * subheading 1
    * subheading 2
</UL>
 * heading 2
 * heading 3
<UL>
    * subheading 1
    * subheading 2
    * subheading 3
</UL>
 * heading 4
 * heading 5
 * heading 6
 * heading 7
<UL>
    * subheading 1
    * subheading 2
    * subheading 3
</UL>
* heading 8
<UL>
    * subheading 1
</UL>
Kenhelm is offline     Reply With Quote


Old 03-17-2009, 06:25 AM   #9
syg00
Guru
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 6,913
Thanked: 166
I am constantly amazed at what people can make sed do.

(same comment applies to awk, and perl ...)
syg00 is offline     Reply With Quote



Reply

Bookmarks


Thread Tools

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
search and replace with multi line string kambrish Programming 5 04-28-2008 07:02 AM
sed search replace tomerbd1 Linux - General 9 04-10-2008 05:31 AM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 06:40 AM
sed search & replace sharathkv25 Programming 2 03-07-2007 11:16 AM
Perl Multi-Line Search & Replace... Can I?... donv2 Linux - Newbie 2 01-30-2007 10:59 PM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration