2

I wonder if there is a sed-only way to print a range of lines, determined by patterns to be matched, except the one last line matching the end pattern.

Consider following example. I have a file

line  1
line  2
line  3
ABC line  4
+ line  5
+ line  6
+ line  7
line  8
line  9
line 10
line 11
line 12

I want to get everything starting with ABC (including) and all the lines beginning with a +:

ABC line  4
+ line  5
+ line  6
+ line  7

I tried it with

sed -n '/ABC/I,/^[^+]/ p' file

but this gives one line too much:

ABC line  4
+ line  5
+ line  6
+ line  7
line  8

What's the easiest way (sed-only) to leave this last line out?

4 Answers 4

5

There might be better ways but I could come up with this sed 1 liner:

sed -rn '/ABC/,/^[^+]/{/(ABC|^\+)/!d;p;}' file

Another sed 1 liner is

sed -n '/ABC/,/^[^+]/{x;/^$/!p;}' file

One more sed 1 liner (and probably better)

sed -n '/ABC/I{h;:A;$!n;/^+/{H;$!bA};g;p;}' file
10
  • @anubhava: Thank you. That's working. It's an interesting idea to solve it this way...
    – bmk
    Apr 28, 2011 at 17:13
  • My pleasure, provided 3 sed 1 liners to achieve the goal.
    – anubhava
    Apr 28, 2011 at 17:23
  • Wow. I will have to have a further look on the 2nd and 3rd versions tomorrow. Currently I do not yet understand them ;) Thank you very much!
    – bmk
    Apr 28, 2011 at 17:29
  • @anubhava: In the meantime I understood those commands. It's really amazing what you can do with a one-line stream-edit-command. I can go with all those versions for my current problem. But the first one has the advantage that it also works with several occurences of ABC..., also when there's no line following in the file and even when there's no line beginning with + following. The disadvantage is that I have to specify the pattern twice.
    – bmk
    Apr 29, 2011 at 5:49
  • 1
    Actually I tried $!ba first but it failed for last line case because there is no next line after last line, so it is important to put $!n also so you are right but both places need $!. I edited the answer again.
    – anubhava
    Apr 29, 2011 at 14:06
2

The easiest way (I'll learn something new if anyone can solve this with one call to sed), is to add an extra sed at the end, i.e.

sed -n '/ABC/I,/^[^+]/ p' file | sed '$d'
ABC line  4
+ line  5
+ line  6
+ line  7

Cheating, I know, but that is the beauty of the Unix pipe philosphy. Keep whitiling down your data until you get what you want ;-)

I hope this helps.

2
  • Yes that works. But i really hoped that there would be a way to do it with one call. But you're right - the pipe philosophy is really nice.
    – bmk
    Apr 28, 2011 at 16:10
  • @bmk : thanks for the vote. I hope you get a complete answer. I'll be intersted to see what it is. I tried /ABC/I,/^[^+]/-1{;p;} which was the only thing I could think that might work, but it didn't. You might have better luck with an ex script. That could still be 1 inline statment, 1 prog invocation AND you can say :wq and not have to mess with managing filenames. Good luck!
    – shellter
    Apr 28, 2011 at 16:23
2

This might work for you:

 sed '/^ABC/{:a;n;/^\(ABC\|+\)/ba};d' file

EDIT: to allow adjacent ABC sections.

1
  • Thank you. That works fine for my given example - therefore +1. Unfortunately in reality there might be several adjacent ABC sections. In that case your sed command would print only every second one.
    – bmk
    Mar 19, 2012 at 10:16
0

Well, you have selected your answer. But why aren't you using /^(ABC|\+)/ ? Or am i mis-understanding your requirement?

If you want to find those + lines AFTER a search for ABC is found

awk '/ABC/{f=1;print} f &&/^\+/ { print }' file

This is much simpler to understand than crafting cryptic sed expressions. When ABC is found, set a flag. When lines starting with + is found and flag is set, print line.

3
  • I cannot use /^(ABC|\+)/ because in this case all lines beginning with + are printed - regardless if they follow an ABC line or not. I guess you are right, that an awk script is a simpler solution but I prefer to use sed for some reasons (e.g. because there's some more processing already done with sed + I'm very interested in getting deeper insights into sed).
    – bmk
    Apr 29, 2011 at 12:34
  • Shouldn't this awk command be: awk '/ABC/{f=1; print} f &&/^\+/ { print }' since OP wants to print those line with ABC also. However I think there still needs to be logic here to toggle f to 0 when lines with ^+ end otherwise even without next instance of ABC it will keep printing all lines with ^+.
    – anubhava
    Apr 29, 2011 at 13:49
  • @anubhava, yes you are right. The ABC should be printed, as per OP's requirement.
    – ghostdog74
    Apr 29, 2011 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.