Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The -i signifies inline editing and also will have sed automatically creates .

If you would like sed to automatically create a backup file . If you want to specify the suffix of the backup file you may add by adding .suffix. For example to create the backup file with the extension .baksedautobck,

Code Block
languagebash
sed -i.baksedautobck 's/cow/reindeer/' hey.txt

...

sed will exit gracefully with a return code of 0 even if a match is found. The sed return code by default is based on running properly not if your data matches.

To determine if there was a match to your file,

One of the problems I've run into, thinking I successfully modified my files even though I did not. Here, I've made a mistake of using the word coy instead of cow,

Code Block
languagebash
sed -i.sedautobck 's/coy/reindeer/' hey.txt
cmp -s hey.txt hey.txt.sedautobck && echo "sed did not work, your files are identical."
sed did not work, your files are identical.

With the mistake you get the extra third line, "sed did not work, your files are identical.".

And if we want to be more fancy to make the cmp line more generic,

Code Block
languagebash
sed -i.sedautobck 's/coy/reindeer/' hey.txt
cmp -s $_ $_.sedautobck && echo "sed did not work, your files are identical."

Insert Multiple Lines from File with Match

...