user1364861
user1364861

Reputation: 321

line replacement in a file in shell scripting

I want to search for the entire line and replace the line with 1 or more lines .

for eg Hi I m there .

I have to replace it with these 2 lines

Hi I m there

Arun is here

Basically what I m trying to do is Insert a new line after the matching line .since I m not sure of line numbers ,thought I would serach for the line after which it should be placed and just replace this line with 2 lines .is it posssible ? the code i used var1 and var are picked up from a csv file and f is the file name

"s@$Var1@$Var2@g" "$f"  

Upvotes: 0

Views: 96

Answers (1)

shellter
shellter

Reputation: 37288

 echo -e "Hi Im here\nLinetwo\nLine3" \
 | sed '/Hi Im here/a\
      Arun is here 

 '

output

 Hi Im here
 Arun is here
 Linetwo
 Line3

This is old-fashioned sed syntax. It should work on any of them.

Note that no space or tab chars allowed after a\, AND that the "a"ppend input is terminated by a blank line, hence the

 Arun is here
 (this is a blank line here, followed by the closing single quote for sed)
 '

IHTH

Upvotes: 3

Related Questions