sed is short for stream editor. It reads in a file (one line at a time), modifies the contents per your parameters, and then outputs.
A more simple description, it allows you to modify the contents of a file without you actually having to open it.
Our sample text file called hey.txt looks like this,
Create this file with the above contents and also create a pristine version. We'll keep on reseting the file by copying back from the pristine version for each sample.
Here a simple example of replacing the word cow with reindeer in the file hey.txt,
The -i signifies inline editing.
If you would like sed to automatically create a backup file specify the suffix of the backup file by adding .suffix. For example to create the backup file with the extension .sedautobck,
Here is an often used example of escaping a the / (slash) which is a reserved character within the single quote,
However, sed permits any character other than backslash or newline to be used instead of the forward slash. For example, the above example may be rewritten as follows to avoid needing to escape the forward slashes (sometimes referred to as "picket fences"):
Detect if No Match Found
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.
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,
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,
Insert Multiple Lines from File with Match
Using sed to edit and save to the same file - http://unstableme.blogspot.ca/2010/01/sed-save-changes-to-same-file.html