Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace/substitute pattern with file content

Tags:

sed

sed -i "/xxxxxxxxxxxx/r inc-sausage" git.html
sed -i "/xxxxxxxxxxxx/d" git.html

First I insert the content of inc-sausage when xxxxxxxxxxxx is found

Second I delete xxxxxxxxxxxx

Both commands do exactly what I want. But how can I combine both sed commands to a single one? I tried

sed -i "s/xxxxxxxxxxxx/r inc-sauasge" git.html
like image 323
vbd Avatar asked Feb 02 '23 10:02

vbd


2 Answers

For starters, you coould concatenate both sed commands into one line and avoid repeating the search string, like this:

sed -i -e "/xxxxxxxxxxxx/r inc-sausage" -e "//d" git.html

Also, if you want to delete xxxxxxxxxxxx only and not other things in its line, you could do that instead:

sed -i -e "/xxxxxxxxxxxx/r inc-sausage" -e "s///" git.html
like image 117
Dalker Avatar answered Feb 05 '23 17:02

Dalker


This might work for you:

sed -i '/xxxxxxxxxxxx/{r inc-sausage'$'\n''d}' git.html

Explanation:

See here why $'\n' is necessary. Also note that d command must be last as it deletes the pattern space and then immediately starts the next cycle.

Or for GNU sed:

sed -i '/xxxxxxxxxxxx/s|.*|cat inc-sausage|e' git.html
like image 41
potong Avatar answered Feb 05 '23 19:02

potong