http://www.grymoire.com/unix/sed.html
Quick Links – NEW
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-6″>/g- Global</td>
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-10a”>/I- Ignore Case</td>
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-9″>/p- Print</td>
</tr>
<tr>
<td>
<a href=”http://www.grymoire.com/unix/Sed.html#uh-10″>/w filename- Write Filename</td>
</tr>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-13″>-e script (–expression=script)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-16″>-f scriptfile (–file=scriptfile)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-19b”>-h (–help)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-15″>-n (–quiet –silent)</td>
</tr>
<tr>
<td><a href=”http://www.grymoire.com/unix/Sed.html#uh-19a”>-V (–version)</td>
</tr>
Copyright 1994,1995 Bruce Barnett and General Electric Company
Copyright 2001,2005,2007,2011,2013 Bruce Barnett
All rights reserved
You are allowed to print copies of this tutorial for your personal use,and link to this page,but you are not allowed to make electronic copies,or redistribute this tutorial in any form without permission.
Original version written in 1994 and published in the Sun Observer
Introduction to Sed
How to use sed,a special editor for modifying files automatically. If you want to write a program to make changes in a file,sed is the tool to use.
There are a few programs that are the real workhorse in the UNIX toolbox. These programs are simple to use for simple applications,yet have a rich set of commands for performing complex actions. Don’t let the complex potential of a program keep you from making use of the simpler aspects. I’ll start with the simple concepts and introduce the advanced topics later on.When I first wrote this,most versions of sed did not allow you to place comments inside the script. Lines starting with the ‘#’ characters are comments. Newer versions of sed may support comments at the end of the line as well.
Sedis the ultimatestreameditor. If that sounds strange,picture a stream flowing through a pipe. Okay,you can’t see a stream if it’s inside a pipe. That’s what I get for attempting a flowing analogy. You want literature,read James Joyce.
Anyhow,sedis a marvelous utility. Unfortunately,most people never learn its real power. The language is very simple,but the documentation is terrible. The Solaris on-line manual pages forsedare five pages long,and two of those pages describe the 34 different errors you can get. A program that spends as much space documenting the errors than it does documenting the language has a serious learning curve.
Do not fret!It is not your fault you don’t understandsed. I will coversedcompletely. But I will describe the features in the order that I learned them. I didn’t learn everything at once. You don’t need to either.
Sedhas several commands,but most people only learn the substitute command:s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing “day” in the “old” file to “night” in the “new” file:
sed s/day/night/new
Or another way (for UNIX beginners),
sed s/day/night/ old >new
and for those who want to test this:
echo day | sed s/day/night/
This will output “night”.
I didn’t put quotes around the argument because this example didn’t need them. If you read my earlier tutorial,you would understand why it doesn’t need quotes. However,I recommend you do use quotes. If you have meta-characters in the command,quotes are necessary. And if you aren’t sure,it’s a good habit,and I will henceforth quote future examples to emphasize the “best practice.” Using the strong (single quote) character,that would be:
sed 's/day/night/'new
I must emphasize that the sed editor changes exactly what you tell it to. So if you executed
echo Sunday | sed 's/day/night/'
This would output the word “Sunnight” because sed found the string “day” in the input.
Another important concept is that sed is line oriented. Suppose you have the input file:
one two three,one two three four three two one one hundred
and you used the command
sed 's/one/ONE/'The output would be
ONE two three,one two three four three two ONE ONE hundredNote that this changed "one" to "ONE" once on each line. The first line had "one" twice,but only the first occurrence was changed. That is the default behavior. If you want something different,you will have to use some of the options that are available. I'll explain them later.
So let's continue.
There are four parts to this substitute command:
s Substitute command /../../ Delimiter one Regular Expression Pattern Search Pattern ONE Replacement stringThe search pattern is on the left hand side and the replacement string is on the right hand side.
We've coveredand. That's 90% of the effort needed to learn the substitute command. To put it another way,you already know how to handle 90% of the most frequent uses ofsed.There are a ... few fine points that any future sed expert should know about. (You just finished section 1. There are only 63 more sections to cover. 🙂 Oh. And you may want to bookmark this page,.... just in case you don't finish.
The character after thesis the delimiter. It is conventionally a slash,because this is whated,more,andviuse. It can be anything you want,however. If you want to change a pathname that contains a slash - say /usr/local/bin to /common/bin - you could use the backslash to quote the slash:
sed 's/\/usr\/local\/bin/\/common\/bin/'new Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to read if you use an underline instead of a slash as a delimiter:
sed 's_/usr/local/bin_/common/bin_'new Some people use colons:
sed 's:/usr/local/bin:/common/bin:'new Others use the "|" character.
sed 's|/usr/local/bin|/common/bin|'new