Skip to content
Advertisement

Problem inserting on first match only using GNU sed

4 on my linux machine (I checked w/ sed --version).

Currently, I have a myfile.txt with the following content:

JavaScript

I know in GNU sed, I can append after the first occurrence of a match if I prepend the sed command with 0,.

So, if I want to insert goodbye after the first occurrence of ---, I can do that:

JavaScript

expected/correct result:

JavaScript

But now, I am trying to insert goodbye after the first occurrence of author: Jan King.

However, the following sed command doesn’t work and appends goodbye 3 times, which is not what I want:

JavaScript

incorrect/unexpected result:

JavaScript

If I remove 0, from the above sed command, then goodbye is appended twice after author: Jan King:

JavaScript

expected result:

JavaScript

So I’m having trouble appending goodbye on first match only of author: Jan King (even though it is fine on first match of ---).

Can someone please explain why my sed command isn’t working? And how to fix it?

Advertisement

Answer

The link you show in the comment is not quite the same command sequence that you’re trying to use. If you want to use the same technique, try:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement