A simple page to describe sed, the stream editor. Originally written and designed for Unix, sed has been ported over to CP/M, MS-DOS, Windows 9x/NT, OS/2, and other operating systems.Please note that our coverage of sed is very much simplified and shows only a small part of its full range of capabilities.I first encountered sed in my CP/M days, when Kaypros ruled the computer world. Basically, sed will change hunks of text on the fly, without making you open up a screen, push a cursor around, and press
DELETEorINSERTorENTERor function keys.How it works: You feed sed a script of prearranged editing commands (like, "change every line that begins with a colon to such-and-such") and sed sends your revised text to the screen. To save the revisions on disk, use the DOS redirection arrow, >
newfile.txt. Sample syntax:sed "one-or-two-sed-commands" input.file >newfile.txt sed -f bigger_sed.script input.file >newfile.txtThis page is an attempt to collect my FAQ (Frequently Asked Questions) files and my favorite sed utilities and introductions for novices. The text files are for any sed user; the binaries are mainly for DOS/Windows users.
...
Eric Pement, sed FAQ maintainer
% cat in
1A A
2A B
3A C
4A D
5A E
1B F
2B G
3B H
4B I
5B J
%
% sed '2,6d' in
1A A
2B G
3B H
4B I
5B J
%
% sed '7,$s/^/=/' < in
1A A
2A B
3A C
4A D
5A E
1B F
=2B G
=3B H
=4B I
=5B J
%
% sed -n '5,8s/[A-E]/Z/gp' in
5Z Z
1Z F
2Z G
3Z H
%
% sed '/^2/,/^4/d' in
1A A
5A E
1B F
5B J
%
% sed -e 's/A/Z/' -e 's/Z.*E/"&"/' in
1Z A
2Z B
3Z C
4Z D
5"Z E"
1B F
2B G
3B H
4B I
5B J
%
% sed -n 's/A/Z/2p' in
1A Z
%
% head -3 in | sed 's/\([^ ]*\) \(.*\)/\2 \1/'
A 1A
B 2A
C 3A
%
% head -3 in | sed -f flip.sed
A 1A
B 2A
C 3A
% cat flip.sed
# Put first word at the end of the line
s/\([^ ]*\) \(.*\)/\2 \1/
%
% ph alias=pazzani | sed -f ph2html.sed
<p><br>
<b>Michael J. PAZZANI</b><br>
Phone: (949) 824-7405, 7403<br>
Information & Computer Science<br>
mailcode: 3425<br>
title: Professor & Chair<br>
Fax: (949) 824-3976<br>
office_address: 444E CS<br>
<img src="http://www.ics.uci.edu/~pazzani/pazzani.gif"><br>
Home Page: <a href="http://www.ics.uci.edu/~pazzani/">http://www.ics.uci.edu/~pazzani/</a><br>
E-mail: pazzani@uci.edu<br>
<p><br>
%
% ph alias=pazzani
----------------------------------------
name: Michael J. PAZZANI
phone: (949) 824-7405, 7403
department: Information & Computer Science
mailcode: 3425
title: Professor & Chair
fax_number: (949) 824-3976
office_address: 444E CS
picture_url: http://www.ics.uci.edu/~pazzani/pazzani.gif
home_page_url: http://www.ics.uci.edu/~pazzani/
email_address: pazzani@uci.edu
----------------------------------------
%
% cat ph2html.sed
/Not present in entry/d
s/^501:.*/No matches found./
s/^[ ]*//
s/^picture_url: \(http:\/\/.*\)/<img src="\1">/
s/^name: \(.*\)/<b>\1<\/b>/
s/^department: //
s/^phone: /Phone: /
s/^fax_number: /Fax: /
s/^email_address: /E-mail: /
s/^home_page_url: \(http:\/\/.*\)/Home Page: <a href="\1">\1<\/a>/
s/^[-]*$/<p>/
s/$/<br>/
%
A command line with one address applies to each line which matches the address. (If that address is a number, rather than a "context address," then the command applies to exactly one line, the line with that number."
A command line with two addresses applies to the (inclusive) range from the first line that matches the first address to the next line that matches the second. Having found a match for the first address, once it finds a following match for the second, sed will start looking for another match for the first address as soon as it has finished with a line which matches the second.
| \ | Combines with following character to give it special meaning or, if it would have had a special meaning without the \, to make it revert to its literal meaning. |
| . | Match any character |
| ^ | Match start of line |
| $ | Match end of line |
| [...] | Match any character in brackets
Example: [abcA-Z7] |
| [^...] | Match
any character except those in brackets
Example: [^abcA-Z7] |
| * | Match 0 or more repetitions of previous item |
| \{m,n\} | Match between m and n occurrences of the immediately preceding character. |
| \{m\} | Match exactly m occurrences of the immediately preceding character. |
| \{m,\} | Match m or more occurrences of the immediately preceding character. |
| \(...\) | Enclosed text is considered a pattern which, if matched, is referred to as \n where n is a digit from 1 to 9. |
| & | In a replacement string, & stands for the full pattern matched. |
% cat Numb
123456789
% sed 's/[1-9]\{3,5\}/+/g' Numb
++
% sed 's/[1-9]\{5,7\}/+/g' Numb
+89
% sed 's/[3-8]\{3,5\}/+/g' Numb
12+89
% sed 's/[3-8]\{3,\}/+/g' Numb
12+9
% sed 's/[3-8]\{3\}/+/g' Numb
12++9
% sed 's/[45][3-8]/+/g' Numb
123+6789
% sed 's/\([45]\)[3-8]/-\1-&-/g' Numb
123-4-45-6789