From charlesreid1

No edit summary
Line 24: Line 24:
</source>
</source>


or, more succinctly:
<source lang="bash">
$ sed -i -e 's/peanut butter/jelly/g;s/green eggs/ham/g' \
        file{1,2,3}.txt
</source>




[[Category:Computers]]
[[Category:Computers]]
[[Category:Programs]]
[[Category:Programs]]

Revision as of 20:56, 18 April 2011

Sed is a *nix system utility that will come with 99% of *nix systems. It's an in-place string manipulation program that can come in handy to make a whole lot of typing into a few lines of string manipulation. It's ugly, but once you start to use it you'll wonder how you ever lived without it.

Sed introduction and tutorial: http://www.grymoire.com/Unix/Sed.html

Editing Files In-Place

Sed can be used to edit files in-place using the -i flag.

Find and Replace

You can find and replace instances of a string in a file using:

$ sed -i -e 's/peanut butter/jelly/g' file{1,2,3}.txt

This replaces peanut butter with jelly in file1.txt, file2,txt, and file3.txt. To replace more than one thing, use

$ sed -i -e 's/peanut butter/jelly/g' \
         -e 's/green eggs/ham/g'      \
         -e 's/water/wine/g'          \
         file{1,2,3}.txt

or, more succinctly:

$ sed -i -e 's/peanut butter/jelly/g;s/green eggs/ham/g' \
         file{1,2,3}.txt