Lgalan90
Lgalan90

Reputation: 615

SED Insert text after a specific multi-line text field

I am looking to search for and add a new line of text after a specific multi-line text, in this example i need to add a space and text after "oldText" under "[old-text]" only:

[old-text]
oldText

[inserted-new-text]
newTxt

[alsoOld-text]
oldText

Here's what I have so far but the syntax is not correct:

printf "[old-text]\noldText"|sed '/\[old-text]\noldTex\t/a [inserted-new-text]\nnewTxt'

Upvotes: 0

Views: 74

Answers (1)

iamauser
iamauser

Reputation: 11469

$ sed -e '/\[old-text\]/{N;s/oldText/&\n\n[inserted-new-text]\nnewTxt/}' inputFile 

Use /<pattern>/ to find the [old-text] and then use N; to go to the next line and replace.

$ printf "[old-text]\noldText" | \
  sed -e '/\[old-text\]/{N;s/oldText/&\n\n[inserted-new-text]\nnewTxt/}'
[old-text]
oldText

[inserted-new-text]
newTxt

Upvotes: 2

Related Questions