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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-13-2009, 08:14 PM
|
#1
|
|
LQ Newbie
Registered: Sep 2004
Posts: 4
Rep:
|
sed multi-line search/replace woes
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
|
|
|
|
03-13-2009, 08:21 PM
|
#2
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,234
|
ignore me - irrelevant post.
Last edited by syg00; 03-13-2009 at 08:26 PM.
|
|
|
|
03-13-2009, 08:30 PM
|
#3
|
|
LQ Newbie
Registered: Sep 2004
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by syg00
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.
|
|
|
|
03-13-2009, 09:21 PM
|
#4
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,234
|
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.
|
|
|
|
03-13-2009, 09:22 PM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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.
|
|
|
|
03-16-2009, 04:25 PM
|
#6
|
|
LQ Newbie
Registered: Sep 2004
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
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!
|
|
|
|
03-16-2009, 05:23 PM
|
#7
|
|
LQ Newbie
Registered: Sep 2004
Posts: 4
Original Poster
Rep:
|
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}'
|
|
|
|
03-17-2009, 05:10 AM
|
#8
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 323
Rep: 
|
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>
|
|
|
|
03-17-2009, 05:25 AM
|
#9
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,234
|
I am constantly amazed at what people can make sed do.
(same comment applies to awk, and perl ...)
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:08 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|