replacing multiple line pattern in 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?


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.


 sed -i.bak '/EXTRATHING/{N;s//SOMETHING/;s/\n//}' input_file.txt

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


perl -lne 's/\s*};\s*$/SOMETHING{};/g if(/\s*};\s*$/);$f=0;if(/EXTRATHING {\s*$/){next;$f=1}else{print}' your_file

tested below:

> cat temp
fjbwjfbw
wefjkbwjk
jbefwejr
EXTRATHING {
};
ghekg
jhgjk
wefjkwngk
EXTRATHING {
  };
jbgvjnrg
jbfw
wefnkwk
EXTRATHING {
};
> perl -lne 's/\s*};\s*$/SOMETHING{};/g if(/\s*};\s*$/);$f=0;if(/EXTRATHING {\s*$/){next;$f=1}else{print}' temp
fjbwjfbw
wefjkbwjk
jbefwejr
SOMETHING{};
ghekg
jhgjk
wefjkwngk
SOMETHING{};
jbgvjnrg
jbfw
wefnkwk
SOMETHING{};
> 

if you want to change the file inline . add a -i flag. like :

perl -i -lne '.......' your_file