Introduction to the sed Editor
The term sed stands for stream editor. Sed can take its input from standard in, apply the requested edits on the stream, and automatically put the results to standard out. The sed syntax allows for an input file to be specified on the command line.
You do not need to interact with the sed editor while it is running; therefore, it has also been termed a batch editor. This is in contrast to such editors as vi and ed, which are interactive. Because sed does not require interaction, you can place sed commands in a script. You can call the script file and run it against the data file to perform repetitive editing operations.
Command Format
The following shows the syntax for the sed command:
# sed [options] 'action [args]' files [ > outfile]
The sed editor is capable of performing text-pattern substitutions and text-pattern deletions using regular expression syntax. These are the same regular expression characters used by grep.
The sed command offers capabilities that are an extension of interactive text editing. If you need to search and replace text strings in a large number of files, sed is most useful.
Editing Commands
The sed editor uses a editing commands (shown in Table) that are similar to those you would use for vi and ed.
Command | Function |
---|---|
d | Deletes line(s) |
p | Prints line(s) |
r | Reads a file |
s | Substitutes one string for another |
w | Writes to a file |
The sed command has two options:
Option | Function |
---|---|
-n | Suppresses the default output |
-f | Reads sed commands from a script file |
Sample data used in the examples
The following examples execute sed commands against the file named “data.txt”. The content of the file data.txt is as follows.
# cat data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster 33 southern SO May Chin 45 southeast SE Derek Johnson 3 eastern EA Susan Beal 34 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Using sed to Print Text
Example 1 : Printing range of lines
The following example shows the use of the p (print) command, which prints a range of lines to stdout. The range is specified by a starting address followed by a comma and then the ending address. The default output of sed is each line that it reads. To suppress the default output, use the -n option.
# sed -n '3,5p' data.txt southwest SW Chris Foster 33 southern SO May Chin 45 southeast SE Derek Johnson 3
Example 2 : Printing lines containing a specific pattern
The following command prints all lines with the pattern west in it. Use the forward slash (/) to delimit the regular expression.
# sed -n '/west/p' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster 33
Example 3 : Printing line with multiple patterns
The following command prints the first line containing the pattern west, up to and including the next line containing the pattern southern.
# sed -n '/west/,/southern/p' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster 33 southern SO May Chin 45
Example 4 : Printing line with pattern and all lines after that
The following command prints the first line containing the pattern Chris, up through the last line of the file.
# sed -n '/Chris/,$p' data.txt southwest SW Chris Foster 33 southern SO May Chin 45 southeast SE Derek Johnson 3 eastern EA Susan Beal 34 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Example 5 : Functionality similar to grep
The pattern might contain the regular expression characters used by grep. The following example prints all lines that begin with an s and end with a 5.
# sed -n '/^s.*5$/p' data.txt southern SO May Chin 45
Using sed to Substitute Text
Command | Example | Editing Action |
---|---|---|
s | sed s/x/y/option | Search and replace. The search pattern x is replaced with pattern y. The search and the replacement pattern are regular expressions in most cases, and the search and replace behavior can be influenced through various options. |
y | sed y/abc/xyz/ | Replace every character from the set of source characters with the character that has the same position in the set of destination characters. |
Example 1 : Search and substitute a specific pattern
The sed s command allows for a search and substitution operation to occur on the text. The command uses a pattern search and a literal string replacement. The replacement string characters are taken literally without metacharacter expansion.
# sed 's/3/X/' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster X3 southern SO May Chin 45 southeast SE Derek Johnson X eastern EA Susan Beal X4 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
The sed command checks each line of the file and substitutes the first occurrence of the old string with the new string. Subsequent occurrences of the old string within the same line are left unchanged.
Example 2 : Search and substitute a specific pattern globally
The following example shows the g (global) command with the s (search and substitute) command, and it replaces all occurrences of the old string with the new string.
# sed 's/3/X/g' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster XX southern SO May Chin 45 southeast SE Derek Johnson X eastern EA Susan Beal X4 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Example 3 : Search and substitute with search pattern included in the substitution
Occasionally with a search and substitute, the old string will be part of the new replacement string, which you can accomplish by placing a & (ampersand) in the replacement string. The location of the & determines the location of the old string in the replacement string.
The objective of the following examples is to write a command that searches for all lines that end with a single digit in the last field and replace the single digit with the single-digit number plus the string Single Digit.
To properly identify the lines with single-digit numbers in the last field, consider the following sed command. Tabs separate the fields with each line.
sed -n '/ [0-9]$/p' data.txt southeast SE Derek Johnson 3
The following command searches for all lines that end with a single digit in the last field and replaces the single digit with the single-digit number plus the string Single Digit.
# sed 's/ [0-9]$/& Single Digit/' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster 33 southern SO May Chin 45 southeast SE Derek Johnson 3 Single Digit eastern EA Susan Beal 34 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Using sed to Delete Text
Example 1 : Delete a range of lines
The following command deletes Lines 4 through 8 from the output.
# sed '4,8d' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 southwest SW Chris Foster 33 central CT Sheri Watson 44
Example 2 : Deleting lines containing a specific pattern
The following command deletes any line containing the pattern west.
# sed '/west/d' data.txt southern SO May Chin 45 southeast SE Derek Johnson 3 eastern EA Susan Beal 34 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Example 3
The following command deletes any line beginning with the pattern west.
# sed '/^west/d' data.txt northwest NW Joel Craig 10 southwest SW Chris Foster 33 southern SO May Chin 45 southeast SE Derek Johnson 3 eastern EA Susan Beal 34 northeast NE TJ Nichols 67 north NO Val Shultz 91 central CT Sheri Watson 44
Example 4
The following command deletes the range of lines beginning with the first line containing the pattern south, up through the next line of the file containing north.
# sed '/south/,/north/d' data.txt northwest NW Joel Craig 10 western WE Sharon Kelly 40 north NO Val Shultz 91 central CT Sheri Watson 44
Additional sed Functionality – Editing Commands
Additional sed editor commands are shown in the following table.
Command | Function |
---|---|
a | Appends text |
c | Changes text in the current line with new text |
i | Inserts text above current line |
Placing Multiple Edits in a Single sed Command
There are times when you want to perform several edits on the file. Rather than using multiple sed commands, you can use the -e option to place the edits in the same command line. The edits are performed in the order that you specify.
Example 1
Delete lines carefully. You might perform edits on a line of text and subsequently remove that line of text from the output. This occurs in the following example with Line 1 of the input file.
# sed -e 's/north/North/' -e '1,4d' data.txt southeast SE Derek Johnson 3 eastern EA Susan Beal 34 Northeast NE TJ Nichols 67 North NO Val Shultz 91 central CT Sheri Watson 44