Home > Commands, Linux, Tech > An introduction to sed

An introduction to sed

sed is a very handy *nix utility for modifying input streams.  It stands for Stream EDitor and it does just that; edits streams.
Its brothers and sisters include awk and grep, which search for content and rearrange output fields.
Let’s start out with an example.  We have a file with the following text in it. Let’s call it “file.txt”:
Bob Marley, 123 Fourth St., Edmonstown, AR, (111) 223-4323
Tom Charley, 234 Fifth St., BobsHouse, NJ, (121) 333-2245
Tim Dickinson, 345 Sixth St., Nasalville, TX, (414) 344-2299
If we want to change St. to Street using sed:
:~> sed ‘s/St./Street/’ file.txt
Bob Marley, 123 Fourth Street, Edmonstown, AR, (111) 223-4323
Tom Charley, 234 Fifth Street, BobsHouse, NJ, (121) 333-2245
Tim Dickinson, 345 Sixth Street, Nasalville, TX, (414) 344-2299
Please note that this does NOT change the original file, it merely pipes out the data to the console.  If you wanted to put it in to a new file you would use the standard redirector > to put it in a new file:
:~> sed ‘s/St./Street/’ file.txt > file2.txt
:~> cat file.txt
Bob Marley, 123 Fourth St., Edmonstown, AR, (111) 223-4323
Tom Charley, 234 Fifth St., BobsHouse, NJ, (121) 333-2245
Tim Dickinson, 345 Sixth St., Nasalville, TX, (414) 344-2299
:~> cat file2.txt
Bob Marley, 123 Fourth Street, Edmonstown, AR, (111) 223-4323
Tom Charley, 234 Fifth Street, BobsHouse, NJ, (121) 333-2245
Tim Dickinson, 345 Sixth Street, Nasalville, TX, (414) 344-2299
“So what’s all this: ‘s/St./Street/’ about anyway?” I hear you ask.  To put it simply, the s stands for “substitute”, the text between the first and second slashes is the text to match, the text between the second and third slashes is the text to replace it with.  It all gets wrapped up in single quotes so that the shell doesn’t eat any spaces or slashes.
What if you want to perform multiple operations on it?
You can precede each operation with a -e, e.g.:
sed -e ‘s/(111)/(222)/’ -e ‘s/St./Street/’ file.txt
You can separate each operation with a semicolon, e.g.:
sed ‘s/(111)/(222)/; s/St./Street/’ file.txt
If you are using a multi-line compatible shell [such as bash] you can press Enter after the first single quote and then after each statement, e.g.:
sed ‘
> s/(111)/(222)/
> s/St./Street/
> s/BobsHouse/Bibbleton/’ file.txt
Alternatively you can create a script file with each operation on a separate line, and then use the -f command to tell it to read the script. For example:
sed -f script.txt file.txt
What if you only want to display the affected lines?  Easy!  Just use the -n parameter, and append p after the operations you want it to print, e.g.:
:~> sed -n -e ‘s/123 Fourth St./12 Third Street/p’ file.txt
Bob Marley, 12 Third Street, Edmonstown, AR, (111) 223-4323
That’s my very basic quick reference guide for sed.
Categories: Commands, Linux, Tech Tags:
  1. No comments yet.
  1. No trackbacks yet.
*