0

I am trying to achieve the following with bash:

Create a file called "file" which contains the following data:

line1
line2
line3456

I want to append additional characters after "line2", for example:

line1
line2 test123
line3456

I know that I can use sed to replace the whole line (sed -i 's/line2/line2 test123/' file). But I am sure that there is a better way to do it.

I intend to modify lines with more than one word in a line so maybe I will need wildcards as well.

sat
  • 14,589
  • 7
  • 46
  • 65
liad9122
  • 291
  • 2
  • 3
  • 9

2 Answers2

2
sed 's/line2/& test123/' file

If you want to edit your file "in place" use sed's option -i.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

Use & as back-reference to the matched pattern:

sed -i 's/word1 word2 word3/& test123/' file

This wil append text " test123" in a line that has word1 word2 word3 text in it.

anubhava
  • 761,203
  • 64
  • 569
  • 643