Sed: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
Sed introduction and tutorial: http://www.grymoire.com/Unix/Sed.html | 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 <code>-i</code> flag. | |||
==Find and Replace== | |||
You can find and replace instances of a string in a file using: | |||
<source lang="bash"> | |||
$ sed -i -e 's/peanut butter/jelly/g' file{1,2,3}.txt | |||
</source> | |||
This replaces peanut butter with jelly in file1.txt, file2,txt, and file3.txt. To replace more than one thing, use | |||
<source lang="bash"> | |||
$ sed -i -e 's/peanut butter/jelly/g' \ | |||
-e 's/green eggs/ham/g' \ | |||
-e 's/water/wine/g' \ | |||
file{1,2,3}.txt | |||
</source> | |||
Revision as of 01:15, 9 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