vbd
vbd

Reputation: 3557

replace/substitute pattern with file content

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

Upvotes: 9

Views: 4968

Answers (2)

potong
potong

Reputation: 58568

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

Upvotes: 3

Dalker
Dalker

Reputation: 538

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

Upvotes: 9

Related Questions