Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

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.

Here a simple example of replacing the word original with new in a file called notes.txt.

sed -i 's/original/new/' notes.txt

The -i signifies inline editing and also will have sed automatically create a backup file.

Here is an often used example of escaping a the / (slash) reserved character within the single quote,

sed -i 's/original\/path/new\/path/' notes.txt # need to escape '/' since we are using it as a separator


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"):

sed -i 's,original/path,new/path,' notes.txt # do not need to escape '/' since we are using ',' as a separator

Insert Multiple Lines from File with Match

..

sed '/cdef/r muliple-lines.txt' notes.txt

..

References

Simple introduction - http://www.grymoire.com/Unix/Sed.html
Another introduction - http://lowfatlinux.com/linux-sed.html

Using sed to edit and save to the same file - http://unstableme.blogspot.ca/2010/01/sed-save-changes-to-same-file.html

  • No labels