Based on the awesome answer found here, I'm trying to use some of these commands inside a sed scriptfile that will be called using sed -f. The file has multiple sed commands and it's been working without a problem. Now I need to add these two lines to the end of the file:

/^$/{N;s/^$\nbegin of this/begin/g}
/^$/{N;s/^$\nend of this/end/g}

which basically searches for an empty line (^$) followed by the next with the phrase begin of this or end of this, and proceeds to delete the empty line and replace the next line just with begin or end.

Each command run independently works without any problems but when put inside a scriptfile, hell breaks lose. Some of my previous commands stop working (i.e. s/\\ \\ //g which just deletes the string \ \). More importantly, only the first, of the previous two multiline commands, is executed, why? Am I missing some command-line switch for sed? Is it possible to have multiple multiline commands in the same scriptfile?

As extra "points", I would love to replace those two regex lines with a capturing group, i.e. /^$/{N;s/^$\n(begin|end) of this/\1/g} but that also doesn't work, not even running it directly on the command line.

I really hope this has a relatively easy/doable solution since I rather not use perl or awk, but if this is a limitation of sed itself or it's extremely complicated to accomplish, I will (very much reluctantly) use the alternatives.

Here's a very basic sample text:

some text

begin of this
some more text

end of this

A sentence

Another sentence 

Which should end up looking like this:

some text
some more text

A sentence 

Another sentence 
  • Hi. Please paste the entire script you have so far and an example of the input file. That would make the issue so much clearer. – seshoumara May 1 at 11:04
  • Let just say for the sake of example that the entire script is just the previous two lines, you'll see that only the first one is executed. I'm running it with sed -r -f. I'll add a sample text to the question – Felipe May 1 at 18:16
  • Do you want just the empty lines before those keywords removed or all? Try this first sed -r '/^$/{$!N;s:^\n(begin|end) of this$:\1:}' file.txt. – seshoumara May 2 at 16:59
  • 1
    Only the first of the two commands is executed because the first command changes the pattern space so that it's no longer empty. Each command is applied to the pattern space, as it is at that point in the script. A /^$/ doesn't "look for" empty lines. If the pattern space is empty at that point in the script, it matches. – Kusalananda May 2 at 18:37
  • @Kusalanda so I should use the hold space too? How do I do that? Or can I just run a single multi-line command with capturing groups? The capturing groups weren't working either – Felipe May 2 at 18:50

I think @seshoumara solved it, right? anyways, I came up with this

sed  -r '/^$/{N; s/\n(begin|end) of this/\1/g}' file
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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