0

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

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

---
title: hello
author: Jan King
foo: bar
author: Jan King
---

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:

 sed -i '0,/---/a goodbye' myfile.txt

expected/correct result:

---
goodbye
title: hello
author: Jan King
foo: bar
author: Jan King
---

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:

 sed -i '0,/^author:.*/a goodbye' myfile.txt

incorrect/unexpected result:

---
goodbye
title: hello
goodbye
author: Jan King
goodbye
foo: bar
author: Jan King
---

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

 sed -i '/^author:.*/a goodbye' myfile.txt

expected result:

---
title: hello
author: Jan King
goodbye
foo: bar
author: Jan King
goodbye
---

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?

kimbaudi
  • 13,655
  • 9
  • 62
  • 74
  • 1
    *I know in GNU sed, I can append after the first occurrence of a match if I prepend the sed command with `0,`* Can you show where you learned this from? It doesn't evidently behave this way. Try, your first example again but put the first occurrence of the `---` on the third line instead of the first. You'll get similar results as your second example. The `0,/...` you are using will append on *every line* up until the matching line. – lurker Aug 07 '19 at 23:32
  • @lurker - https://stackoverflow.com/a/9970467/1097123 https://stackoverflow.com/a/148473/1097123 https://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq4_004.html – kimbaudi Aug 07 '19 at 23:34
  • See this post: [What is the meaning of “0,/xxx” in sed?](https://stackoverflow.com/questions/31869731/what-is-the-meaning-of-0-xxx-in-sed). It indicates that *`0,/^ENABLE_DEBUG.*/` means that the substitution will only occur **on lines from the beginning***. The link you show is not quite the same command sequence that you're using. – lurker Aug 07 '19 at 23:36

3 Answers3

2

You need to use the two conditions:

  1. the range of lines 0,/^author:/
  2. the specific line inside this range /^author:/

You can write it like this:

sed -i '0,/^author:/{/^author:/a goodbye
}' file
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

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:

sed -i '0,/^\(author:.*\)/s//\1\nNew Inserted Line/' myfile.txt
lurker
  • 56,987
  • 9
  • 69
  • 103
1

This might work for you (GNU sed):

sed -i -e '/^author:/{a goodbye' -e ':a;n;ba}' file

Focus on the first line containing author:, then append goodbye and loop through all remaining lines.

kimbaudi
  • 13,655
  • 9
  • 62
  • 74
potong
  • 55,640
  • 6
  • 51
  • 83