ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have to say, I learned a lot about sed by solving this one!
Code:
/FOLLOW ME$/ {
p
c\
n
bx
}
p
d
:x
{
/The END$/ ! p
c\
n
bx
}
It's kind of a cheap move since sed is stateless, but what we basically do is find the "FOLLOW ME" then enter into a loop constantly clearing and replacing the contents of the pattern space with the next line. This only goes through as many cycles as there are lines before the "FOLLOW ME", then after that it loops within the current cycle gathering up lines until EOF.
Note that this would be much more clearly implemented in awk, but since you asked for sed...
Last edited by taylor_venable; 01-02-2009 at 02:25 PM.
Reason: Added explanation.
Just realized a semantic ambiguity.....my solution starts on the first instance of "follow me", and then deletes all instances of "the end" at the end of subsequent lines.
If you meant delete lines ending in "the end" ONLY if the immediately follow one with "follow me", then my solution does not work
Ha ha! The simple solution completely evaded me! At least the incredibly over-engineered solution provides a starting point for doing more complex things like only deleting lines which appear immediately after "FOLLOW ME" but I must confess I wasn't considering this when I spent fifteen minutes writing it.
I have to say I was very pleased when I found out you can use labels and branches in sed. Crazy.
Edit: Ooh, I just tested my complicated solution with GNU sed and it does... basically nothing. Upon further investigation, it seems that GNU sed never branches down to the label after reading "FOLLOW ME"; a new cycle starts immediately after the n command. The posted solution works correctly with BSD sed (or at least the one in OpenBSD).
Last edited by taylor_venable; 01-02-2009 at 08:07 PM.
Reason: Add GNU sed caveat.
Posts that include a sample data set and desired output (inside CODE tags) help to avoid ambiguity (but may also avoid the flights of serendipitous learning while chasing the ambiguity ) ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.