Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substituting a single line with multiple lines of text

Tags:

linux

shell

In Linux what command can I use to replace a single line of text with new multiple lines? I want to look for a keyword on a line and delete this line and replace it with multiple new lines. So in the text shown below I want to search for the line that contains "keyword" and replace the entire line with 3 new lines of text as shown.

For example replacing the line containing the keyword,

This is Line 1
This is Line 2 that has keyword
This is Line 3

changed to this:

This is Line 1
Inserted is new first line
Inserted is new second line
Inserted is new third line
This is Line 3
like image 315
SSS Avatar asked Mar 06 '12 00:03

SSS


1 Answers

$ sed '/keyword/c\
> Inserted is new first line\
> Inserted is new second line\
> Inserted is new third line' input.txt

This is Line 1
Inserted is new first line
Inserted is new second line
Inserted is new third line
This is Line 3

$ and > are bash prompt

like image 112
kev Avatar answered Nov 24 '22 21:11

kev