Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing multiple line pattern in sed

Tags:

sed

I simply want to do following

replace

EXTRATHING {
};

by

SOMETHING {};

in inputfile. For this, I tried

sed -e 's/EXTRATHING {\n};/SOMETHING/' input_file.txt  >outfile.txt

This doesn't work. Can someone suggest what would be the correct way of doing this with sed?

like image 757
bbv Avatar asked Nov 17 '11 09:11

bbv


3 Answers

sed -n '1h;1!H;${;g;s/EXTRATHING {\n};/SOMETHING {};/g;p;}' input_file.txt

would do it.

The problem with this is that it stores the whole input string in sed's buffer.

See sed and Multi-Line Search and Replace for more info, and a more efficient version.

like image 58
Bertrand Marron Avatar answered Jan 03 '23 16:01

Bertrand Marron


 sed -i.bak '/EXTRATHING/{N;s//SOMETHING/;s/\n//}' input_file.txt
like image 23
Sidharth C. Nadhan Avatar answered Jan 03 '23 18:01

Sidharth C. Nadhan


sed -e '/begin/,/end/{s/begin/replacement/p;d}'

like image 37
BCS Avatar answered Jan 03 '23 16:01

BCS