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.
Basics
A more simple description, it allows you to modify the contents of a file without you actually having to open it.
Basics
Our sample text file called hey.txt looks like this,
Code Block |
---|
Hey, diddle, diddle,
The cat and the fiddle,
The cow jumped over the moon;
The little dog laughed
To see such sport,
And the dish ran away with the spoon.
Make sure to keep this file in your /opt/nursery/ directory. |
Here a simple example of replacing the word original cow with new in a file called notesreindeer in the file hey.txt.,
Code Block | ||
---|---|---|
| ||
sed -i 's/originalcow/newreindeer/' notes.txt |
The -i signifies inline editing and also will have sed automatically create creates a backup file.
Here is an often used example of escaping a the / (slash) reserved character within the single quote,
Code Block | ||
---|---|---|
| ||
sed -i 's/originalopt\/pathnursery/newvar\/pathryhme/' notes.txt # need to escape '/' since we are using it as a separator |
...