<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wikis.ch.cam.ac.uk/ro-walesdocs/wiki/index.php?action=history&amp;feed=atom&amp;title=Short_%27sed%27_examples</id>
	<title>Short &#039;sed&#039; examples - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wikis.ch.cam.ac.uk/ro-walesdocs/wiki/index.php?action=history&amp;feed=atom&amp;title=Short_%27sed%27_examples"/>
	<link rel="alternate" type="text/html" href="https://wikis.ch.cam.ac.uk/ro-walesdocs/wiki/index.php?title=Short_%27sed%27_examples&amp;action=history"/>
	<updated>2026-04-13T09:32:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.7</generator>
	<entry>
		<id>https://wikis.ch.cam.ac.uk/ro-walesdocs/wiki/index.php?title=Short_%27sed%27_examples&amp;diff=1336&amp;oldid=prev</id>
		<title>Adk44: Created page with &quot;The examples below were prepared by Eric Pement and taken from this [http://sed.sourceforge.net/sed1line.txt web page]. A detailed description can be found [http://www.gnu.org...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wikis.ch.cam.ac.uk/ro-walesdocs/wiki/index.php?title=Short_%27sed%27_examples&amp;diff=1336&amp;oldid=prev"/>
		<updated>2019-05-13T12:39:18Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;The examples below were prepared by Eric Pement and taken from this [http://sed.sourceforge.net/sed1line.txt web page]. A detailed description can be found [http://www.gnu.org...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The examples below were prepared by Eric Pement and taken from this [http://sed.sourceforge.net/sed1line.txt web page]. A detailed description can be found [http://www.gnu.org/software/sed/manual/sed.html here] in the &amp;#039;&amp;#039;sed&amp;#039;&amp;#039; manual. Although this is only a short list of commands, you&amp;#039;ll find that 99% of the time, you will be able to alter one to do what you need!&lt;br /&gt;
&lt;br /&gt;
FILE SPACING:&lt;br /&gt;
&lt;br /&gt;
 # double space a file&lt;br /&gt;
 sed G&lt;br /&gt;
&lt;br /&gt;
 # double space a file which already has blank lines in it. Output file&lt;br /&gt;
 # should contain no more than one blank line between lines of text.&lt;br /&gt;
 sed &amp;#039;/^$/d;G&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # triple space a file&lt;br /&gt;
 sed &amp;#039;G;G&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # undo double-spacing (assumes even-numbered lines are always blank)&lt;br /&gt;
 sed &amp;#039;n;d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # insert a blank line above every line which matches &amp;quot;regex&amp;quot;&lt;br /&gt;
 sed &amp;#039;/regex/{x;p;x;}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # insert a blank line below every line which matches &amp;quot;regex&amp;quot;&lt;br /&gt;
 sed &amp;#039;/regex/G&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # insert a blank line above and below every line which matches &amp;quot;regex&amp;quot;&lt;br /&gt;
 sed &amp;#039;/regex/{x;p;x;G;}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
NUMBERING:&lt;br /&gt;
&lt;br /&gt;
 # number each line of a file (simple left alignment). Using a tab (see&lt;br /&gt;
 # note on &amp;#039;\t&amp;#039; at end of file) instead of space will preserve margins.&lt;br /&gt;
 sed = filename | sed &amp;#039;N;s/\n/\t/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # number each line of a file (number on left, right-aligned)&lt;br /&gt;
 sed = filename | sed &amp;#039;N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # number each line of file, but only print numbers if line is not blank&lt;br /&gt;
 sed &amp;#039;/./=&amp;#039; filename | sed &amp;#039;/./N; s/\n/ /&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # count lines (emulates &amp;quot;wc -l&amp;quot;)&lt;br /&gt;
 sed -n &amp;#039;$=&amp;#039;&lt;br /&gt;
&lt;br /&gt;
TEXT CONVERSION AND SUBSTITUTION:&lt;br /&gt;
&lt;br /&gt;
 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.&lt;br /&gt;
 sed &amp;#039;s/.$//&amp;#039;               # assumes that all lines end with CR/LF&lt;br /&gt;
 sed &amp;#039;s/^M$//&amp;#039;              # in bash/tcsh, press Ctrl-V then Ctrl-M&lt;br /&gt;
 sed &amp;#039;s/\x0D$//&amp;#039;            # works on ssed, gsed 3.02.80 or higher&lt;br /&gt;
&lt;br /&gt;
 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.&lt;br /&gt;
 sed &amp;quot;s/$/`echo -e \\\r`/&amp;quot;            # command line under ksh&lt;br /&gt;
 sed &amp;#039;s/$&amp;#039;&amp;quot;/`echo \\\r`/&amp;quot;             # command line under bash&lt;br /&gt;
 sed &amp;quot;s/$/`echo \\\r`/&amp;quot;               # command line under zsh&lt;br /&gt;
 sed &amp;#039;s/$/\r/&amp;#039;                        # gsed 3.02.80 or higher&lt;br /&gt;
&lt;br /&gt;
 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.&lt;br /&gt;
 sed &amp;quot;s/$//&amp;quot;                          # method 1&lt;br /&gt;
 sed -n p                             # method 2&lt;br /&gt;
&lt;br /&gt;
 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.&lt;br /&gt;
 # Can only be done with UnxUtils sed, version 4.0.7 or higher. The&lt;br /&gt;
 # UnxUtils version can be identified by the custom &amp;quot;--text&amp;quot; switch&lt;br /&gt;
 # which appears when you use the &amp;quot;--help&amp;quot; switch. Otherwise, changing&lt;br /&gt;
 # DOS newlines to Unix newlines cannot be done with sed in a DOS&lt;br /&gt;
 # environment. Use &amp;quot;tr&amp;quot; instead.&lt;br /&gt;
 sed &amp;quot;s/\r//&amp;quot; infile &amp;gt;outfile         # UnxUtils sed v4.0.7 or higher&lt;br /&gt;
 tr -d \r &amp;lt;infile &amp;gt;outfile            # GNU tr version 1.22 or higher&lt;br /&gt;
&lt;br /&gt;
 # delete leading whitespace (spaces, tabs) from front of each line&lt;br /&gt;
 # aligns all text flush left&lt;br /&gt;
 sed &amp;#039;s/^[ \t]*//&amp;#039;                    # see note on &amp;#039;\t&amp;#039; at end of file&lt;br /&gt;
&lt;br /&gt;
 # delete trailing whitespace (spaces, tabs) from end of each line&lt;br /&gt;
 sed &amp;#039;s/[ \t]*$//&amp;#039;                    # see note on &amp;#039;\t&amp;#039; at end of file&lt;br /&gt;
&lt;br /&gt;
 # delete BOTH leading and trailing whitespace from each line&lt;br /&gt;
 sed &amp;#039;s/^[ \t]*//;s/[ \t]*$//&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # insert 5 blank spaces at beginning of each line (make page offset)&lt;br /&gt;
 sed &amp;#039;s/^/     /&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # align all text flush right on a 79-column width&lt;br /&gt;
 sed -e :a -e &amp;#039;s/^.\{1,78\}$/ &amp;amp;/;ta&amp;#039;  # set at 78 plus 1 space&lt;br /&gt;
&lt;br /&gt;
 # center all text in the middle of 79-column width. In method 1,&lt;br /&gt;
 # spaces at the beginning of the line are significant, and trailing&lt;br /&gt;
 # spaces are appended at the end of the line. In method 2, spaces at&lt;br /&gt;
 # the beginning of the line are discarded in centering the line, and&lt;br /&gt;
 # no trailing spaces appear at the end of lines.&lt;br /&gt;
 sed  -e :a -e &amp;#039;s/^.\{1,77\}$/ &amp;amp; /;ta&amp;#039;                     # method 1&lt;br /&gt;
 sed  -e :a -e &amp;#039;s/^.\{1,77\}$/ &amp;amp;/;ta&amp;#039; -e &amp;#039;s/\( *\)\1/\1/&amp;#039;  # method 2&lt;br /&gt;
&lt;br /&gt;
 # substitute (find and replace) &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; on each line&lt;br /&gt;
 sed &amp;#039;s/foo/bar/&amp;#039;             # replaces only 1st instance in a line&lt;br /&gt;
 sed &amp;#039;s/foo/bar/4&amp;#039;            # replaces only 4th instance in a line&lt;br /&gt;
 sed &amp;#039;s/foo/bar/g&amp;#039;            # replaces ALL instances in a line&lt;br /&gt;
 sed &amp;#039;s/\(.*\)foo\(.*foo\)/\1bar\2/&amp;#039; # replace the next-to-last case&lt;br /&gt;
 sed &amp;#039;s/\(.*\)foo/\1bar/&amp;#039;            # replace only the last case&lt;br /&gt;
&lt;br /&gt;
 # substitute &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; ONLY for lines which contain &amp;quot;baz&amp;quot;&lt;br /&gt;
 sed &amp;#039;/baz/s/foo/bar/g&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # substitute &amp;quot;foo&amp;quot; with &amp;quot;bar&amp;quot; EXCEPT for lines which contain &amp;quot;baz&amp;quot;&lt;br /&gt;
 sed &amp;#039;/baz/!s/foo/bar/g&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # change &amp;quot;scarlet&amp;quot; or &amp;quot;ruby&amp;quot; or &amp;quot;puce&amp;quot; to &amp;quot;red&amp;quot;&lt;br /&gt;
 sed &amp;#039;s/scarlet/red/g;s/ruby/red/g;s/puce/red/g&amp;#039;   # most seds&lt;br /&gt;
 gsed &amp;#039;s/scarlet\|ruby\|puce/red/g&amp;#039;                # GNU sed only&lt;br /&gt;
&lt;br /&gt;
 # reverse order of lines (emulates &amp;quot;tac&amp;quot;)&lt;br /&gt;
 # bug/feature in HHsed v1.5 causes blank lines to be deleted&lt;br /&gt;
 sed &amp;#039;1!G;h;$!d&amp;#039;               # method 1&lt;br /&gt;
 sed -n &amp;#039;1!G;h;$p&amp;#039;             # method 2&lt;br /&gt;
&lt;br /&gt;
 # reverse each character on the line (emulates &amp;quot;rev&amp;quot;)&lt;br /&gt;
 sed &amp;#039;/\n/!G;s/\(.\)\(.*\n\)/&amp;amp;\2\1/;//D;s/.//&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # join pairs of lines side-by-side (like &amp;quot;paste&amp;quot;)&lt;br /&gt;
 sed &amp;#039;$!N;s/\n/ /&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # if a line ends with a backslash, append the next line to it&lt;br /&gt;
 sed -e :a -e &amp;#039;/\\$/N; s/\\\n//; ta&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # if a line begins with an equal sign, append it to the previous line&lt;br /&gt;
 # and replace the &amp;quot;=&amp;quot; with a single space&lt;br /&gt;
 sed -e :a -e &amp;#039;$!N;s/\n=/ /;ta&amp;#039; -e &amp;#039;P;D&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # add commas to numeric strings, changing &amp;quot;1234567&amp;quot; to &amp;quot;1,234,567&amp;quot;&lt;br /&gt;
 gsed &amp;#039;:a;s/\B[0-9]\{3\}\&amp;gt;/,&amp;amp;/;ta&amp;#039;                     # GNU sed&lt;br /&gt;
 sed -e :a -e &amp;#039;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta&amp;#039;  # other seds&lt;br /&gt;
&lt;br /&gt;
 # add commas to numbers with decimal points and minus signs (GNU sed)&lt;br /&gt;
 gsed -r &amp;#039;:a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)&lt;br /&gt;
 gsed &amp;#039;0~5G&amp;#039;                  # GNU sed only&lt;br /&gt;
 sed &amp;#039;n;n;n;n;G;&amp;#039;             # other seds&lt;br /&gt;
&lt;br /&gt;
SELECTIVE PRINTING OF CERTAIN LINES:&lt;br /&gt;
&lt;br /&gt;
 # print first 10 lines of file (emulates behavior of &amp;quot;head&amp;quot;)&lt;br /&gt;
 sed 10q&lt;br /&gt;
&lt;br /&gt;
 # print first line of file (emulates &amp;quot;head -1&amp;quot;)&lt;br /&gt;
 sed q&lt;br /&gt;
&lt;br /&gt;
 # print the last 10 lines of a file (emulates &amp;quot;tail&amp;quot;)&lt;br /&gt;
 sed -e :a -e &amp;#039;$q;N;11,$D;ba&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the last 2 lines of a file (emulates &amp;quot;tail -2&amp;quot;)&lt;br /&gt;
 sed &amp;#039;$!N;$!D&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the last line of a file (emulates &amp;quot;tail -1&amp;quot;)&lt;br /&gt;
 sed &amp;#039;$!d&amp;#039;                    # method 1&lt;br /&gt;
 sed -n &amp;#039;$p&amp;#039;                  # method 2&lt;br /&gt;
&lt;br /&gt;
 # print the next-to-the-last line of a file&lt;br /&gt;
 sed -e &amp;#039;$!{h;d;}&amp;#039; -e x              # for 1-line files, print blank line&lt;br /&gt;
 sed -e &amp;#039;1{$q;}&amp;#039; -e &amp;#039;$!{h;d;}&amp;#039; -e x  # for 1-line files, print the line&lt;br /&gt;
 sed -e &amp;#039;1{$d;}&amp;#039; -e &amp;#039;$!{h;d;}&amp;#039; -e x  # for 1-line files, print nothing&lt;br /&gt;
&lt;br /&gt;
 # print only lines which match regular expression (emulates &amp;quot;grep&amp;quot;)&lt;br /&gt;
 sed -n &amp;#039;/regexp/p&amp;#039;           # method 1&lt;br /&gt;
 sed &amp;#039;/regexp/!d&amp;#039;             # method 2&lt;br /&gt;
&lt;br /&gt;
 # print only lines which do NOT match regexp (emulates &amp;quot;grep -v&amp;quot;)&lt;br /&gt;
 sed -n &amp;#039;/regexp/!p&amp;#039;          # method 1, corresponds to above&lt;br /&gt;
 sed &amp;#039;/regexp/d&amp;#039;              # method 2, simpler syntax&lt;br /&gt;
&lt;br /&gt;
 # print the line immediately before a regexp, but not the line&lt;br /&gt;
 # containing the regexp&lt;br /&gt;
 sed -n &amp;#039;/regexp/{g;1!p;};h&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print the line immediately after a regexp, but not the line&lt;br /&gt;
 # containing the regexp&lt;br /&gt;
 sed -n &amp;#039;/regexp/{n;p;}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print 1 line of context before and after regexp, with line number&lt;br /&gt;
 # indicating where the regexp occurred (similar to &amp;quot;grep -A1 -B1&amp;quot;)&lt;br /&gt;
 sed -n -e &amp;#039;/regexp/{=;x;1!p;g;$!N;p;D;}&amp;#039; -e h&lt;br /&gt;
&lt;br /&gt;
 # grep for AAA and BBB and CCC (in any order)&lt;br /&gt;
 sed &amp;#039;/AAA/!d; /BBB/!d; /CCC/!d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # grep for AAA and BBB and CCC (in that order)&lt;br /&gt;
 sed &amp;#039;/AAA.*BBB.*CCC/!d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # grep for AAA or BBB or CCC (emulates &amp;quot;egrep&amp;quot;)&lt;br /&gt;
 sed -e &amp;#039;/AAA/b&amp;#039; -e &amp;#039;/BBB/b&amp;#039; -e &amp;#039;/CCC/b&amp;#039; -e d    # most seds&lt;br /&gt;
 gsed &amp;#039;/AAA\|BBB\|CCC/!d&amp;#039;                        # GNU sed only&lt;br /&gt;
&lt;br /&gt;
 # print paragraph if it contains AAA (blank lines separate paragraphs)&lt;br /&gt;
 # HHsed v1.5 must insert a &amp;#039;G;&amp;#039; after &amp;#039;x;&amp;#039; in the next 3 scripts below&lt;br /&gt;
 sed -e &amp;#039;/./{H;$!d;}&amp;#039; -e &amp;#039;x;/AAA/!d;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print paragraph if it contains AAA and BBB and CCC (in any order)&lt;br /&gt;
 sed -e &amp;#039;/./{H;$!d;}&amp;#039; -e &amp;#039;x;/AAA/!d;/BBB/!d;/CCC/!d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print paragraph if it contains AAA or BBB or CCC&lt;br /&gt;
 sed -e &amp;#039;/./{H;$!d;}&amp;#039; -e &amp;#039;x;/AAA/b&amp;#039; -e &amp;#039;/BBB/b&amp;#039; -e &amp;#039;/CCC/b&amp;#039; -e d&lt;br /&gt;
 gsed &amp;#039;/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d&amp;#039;         # GNU sed only&lt;br /&gt;
&lt;br /&gt;
 # print only lines of 65 characters or longer&lt;br /&gt;
 sed -n &amp;#039;/^.\{65\}/p&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print only lines of less than 65 characters&lt;br /&gt;
 sed -n &amp;#039;/^.\{65\}/!p&amp;#039;        # method 1, corresponds to above&lt;br /&gt;
 sed &amp;#039;/^.\{65\}/d&amp;#039;            # method 2, simpler syntax&lt;br /&gt;
&lt;br /&gt;
 # print section of file from regular expression to end of file&lt;br /&gt;
 sed -n &amp;#039;/regexp/,$p&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # print section of file based on line numbers (lines 8-12, inclusive)&lt;br /&gt;
 sed -n &amp;#039;8,12p&amp;#039;               # method 1&lt;br /&gt;
 sed &amp;#039;8,12!d&amp;#039;                 # method 2&lt;br /&gt;
&lt;br /&gt;
 # print line number 52&lt;br /&gt;
 sed -n &amp;#039;52p&amp;#039;                 # method 1&lt;br /&gt;
 sed &amp;#039;52!d&amp;#039;                   # method 2&lt;br /&gt;
 sed &amp;#039;52q;d&amp;#039;                  # method 3, efficient on large files&lt;br /&gt;
&lt;br /&gt;
 # beginning at line 3, print every 7th line&lt;br /&gt;
 gsed -n &amp;#039;3~7p&amp;#039;               # GNU sed only&lt;br /&gt;
 sed -n &amp;#039;3,${p;n;n;n;n;n;n;}&amp;#039; # other seds&lt;br /&gt;
&lt;br /&gt;
 # print section of file between two regular expressions (inclusive)&lt;br /&gt;
 sed -n &amp;#039;/Iowa/,/Montana/p&amp;#039;             # case sensitive&lt;br /&gt;
&lt;br /&gt;
SELECTIVE DELETION OF CERTAIN LINES:&lt;br /&gt;
&lt;br /&gt;
 # print all of file EXCEPT section between 2 regular expressions&lt;br /&gt;
 sed &amp;#039;/Iowa/,/Montana/d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete duplicate, consecutive lines from a file (emulates &amp;quot;uniq&amp;quot;).&lt;br /&gt;
 # First line in a set of duplicate lines is kept, rest are deleted.&lt;br /&gt;
 sed &amp;#039;$!N; /^\(.*\)\n\1$/!P; D&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete duplicate, nonconsecutive lines from a file. Beware not to&lt;br /&gt;
 # overflow the buffer size of the hold space, or else use GNU sed.&lt;br /&gt;
 sed -n &amp;#039;G; s/\n/&amp;amp;&amp;amp;/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete all lines except duplicate lines (emulates &amp;quot;uniq -d&amp;quot;).&lt;br /&gt;
 sed &amp;#039;$!N; s/^\(.*\)\n\1$/\1/; t; D&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete the first 10 lines of a file&lt;br /&gt;
 sed &amp;#039;1,10d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete the last line of a file&lt;br /&gt;
 sed &amp;#039;$d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete the last 2 lines of a file&lt;br /&gt;
 sed &amp;#039;N;$!P;$!D;$d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete the last 10 lines of a file&lt;br /&gt;
 sed -e :a -e &amp;#039;$d;N;2,10ba&amp;#039; -e &amp;#039;P;D&amp;#039;   # method 1&lt;br /&gt;
 sed -n -e :a -e &amp;#039;1,10!{P;N;D;};N;ba&amp;#039;  # method 2&lt;br /&gt;
&lt;br /&gt;
 # delete every 8th line&lt;br /&gt;
 gsed &amp;#039;0~8d&amp;#039;                           # GNU sed only&lt;br /&gt;
 sed &amp;#039;n;n;n;n;n;n;n;d;&amp;#039;                # other seds&lt;br /&gt;
&lt;br /&gt;
 # delete lines matching pattern&lt;br /&gt;
 sed &amp;#039;/pattern/d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete ALL blank lines from a file (same as &amp;quot;grep &amp;#039;.&amp;#039; &amp;quot;)&lt;br /&gt;
 sed &amp;#039;/^$/d&amp;#039;                           # method 1&lt;br /&gt;
 sed &amp;#039;/./!d&amp;#039;                           # method 2&lt;br /&gt;
&lt;br /&gt;
 # delete all CONSECUTIVE blank lines from file except the first; also&lt;br /&gt;
 # deletes all blank lines from top and end of file (emulates &amp;quot;cat -s&amp;quot;)&lt;br /&gt;
 sed &amp;#039;/./,/^$/!d&amp;#039;          # method 1, allows 0 blanks at top, 1 at EOF&lt;br /&gt;
 sed &amp;#039;/^$/N;/\n$/D&amp;#039;        # method 2, allows 1 blank at top, 0 at EOF&lt;br /&gt;
&lt;br /&gt;
 # delete all CONSECUTIVE blank lines from file except the first 2:&lt;br /&gt;
 sed &amp;#039;/^$/N;/\n$/N;//D&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete all leading blank lines at top of file&lt;br /&gt;
 sed &amp;#039;/./,$!d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete all trailing blank lines at end of file&lt;br /&gt;
 sed -e :a -e &amp;#039;/^\n*$/{$d;N;ba&amp;#039; -e &amp;#039;}&amp;#039;  # works on all seds&lt;br /&gt;
 sed -e :a -e &amp;#039;/^\n*$/N;/\n$/ba&amp;#039;        # ditto, except for gsed 3.02.*&lt;br /&gt;
&lt;br /&gt;
 # delete the last line of each paragraph&lt;br /&gt;
 sed -n &amp;#039;/^$/{p;h;};/./{x;/./p;}&amp;#039;&lt;br /&gt;
&lt;br /&gt;
SPECIAL APPLICATIONS:&lt;br /&gt;
&lt;br /&gt;
 # remove nroff overstrikes (char, backspace) from man pages. The &amp;#039;echo&amp;#039;&lt;br /&gt;
 # command may need an -e switch if you use Unix System V or bash shell.&lt;br /&gt;
 sed &amp;quot;s/.`echo \\\b`//g&amp;quot;    # double quotes required for Unix environment&lt;br /&gt;
 sed &amp;#039;s/.^H//g&amp;#039;             # in bash/tcsh, press Ctrl-V and then Ctrl-H&lt;br /&gt;
 sed &amp;#039;s/.\x08//g&amp;#039;           # hex expression for sed 1.5, GNU sed, ssed&lt;br /&gt;
&lt;br /&gt;
 # get Usenet/e-mail message header&lt;br /&gt;
 sed &amp;#039;/^$/q&amp;#039;                # deletes everything after first blank line&lt;br /&gt;
&lt;br /&gt;
 # get Usenet/e-mail message body&lt;br /&gt;
 sed &amp;#039;1,/^$/d&amp;#039;              # deletes everything up to first blank line&lt;br /&gt;
&lt;br /&gt;
 # get Subject header, but remove initial &amp;quot;Subject: &amp;quot; portion&lt;br /&gt;
 sed &amp;#039;/^Subject: */!d; s///;q&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # get return address header&lt;br /&gt;
 sed &amp;#039;/^Reply-To:/q; /^From:/h; /./d;g;q&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # parse out the address proper. Pulls out the e-mail address by itself&lt;br /&gt;
 # from the 1-line return address header (see preceding script)&lt;br /&gt;
 sed &amp;#039;s/ *(.*)//; s/&amp;gt;.*//; s/.*[:&amp;lt;] *//&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # add a leading angle bracket and space to each line (quote a message)&lt;br /&gt;
 sed &amp;#039;s/^/&amp;gt; /&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # delete leading angle bracket &amp;amp; space from each line (unquote a message)&lt;br /&gt;
 sed &amp;#039;s/^&amp;gt; //&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # remove most HTML tags (accommodates multiple-line tags)&lt;br /&gt;
 sed -e :a -e &amp;#039;s/&amp;lt;[^&amp;gt;]*&amp;gt;//g;/&amp;lt;/N;//ba&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # extract multi-part uuencoded binaries, removing extraneous header&lt;br /&gt;
 # info, so that only the uuencoded portion remains. Files passed to&lt;br /&gt;
 # sed must be passed in the proper order. Version 1 can be entered&lt;br /&gt;
 # from the command line; version 2 can be made into an executable&lt;br /&gt;
 # Unix shell script. (Modified from a script by Rahul Dhesi.)&lt;br /&gt;
 sed &amp;#039;/^end/,/^begin/d&amp;#039; file1 file2 ... fileX | uudecode   # vers. 1&lt;br /&gt;
 sed &amp;#039;/^end/,/^begin/d&amp;#039; &amp;quot;$@&amp;quot; | uudecode                    # vers. 2&lt;br /&gt;
&lt;br /&gt;
 # sort paragraphs of file alphabetically. Paragraphs are separated by blank&lt;br /&gt;
 # lines. GNU sed uses \v for vertical tab, or any unique char will do.&lt;br /&gt;
 sed &amp;#039;/./{H;d;};x;s/\n/={NL}=/g&amp;#039; file | sort | sed &amp;#039;1s/={NL}=//;s/={NL}=/\n/g&amp;#039;&lt;br /&gt;
 gsed &amp;#039;/./{H;d};x;y/\n/\v/&amp;#039; file | sort | sed &amp;#039;1s/\v//;y/\v/\n/&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 # zip up each .TXT file individually, deleting the source file and&lt;br /&gt;
 # setting the name of each .ZIP file to the basename of the .TXT file&lt;br /&gt;
 # (under DOS: the &amp;quot;dir /b&amp;quot; switch returns bare filenames in all caps).&lt;br /&gt;
 echo @echo off &amp;gt;zipup.bat&lt;br /&gt;
 dir /b *.txt | sed &amp;quot;s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/&amp;quot; &amp;gt;&amp;gt;zipup.bat&lt;br /&gt;
&lt;br /&gt;
TYPICAL USE: Sed takes one or more editing commands and applies all of&lt;br /&gt;
them, in sequence, to each line of input. After all the commands have&lt;br /&gt;
been applied to the first input line, that line is output and a second&lt;br /&gt;
input line is taken for processing, and the cycle repeats. The&lt;br /&gt;
preceding examples assume that input comes from the standard input&lt;br /&gt;
device (i.e, the console, normally this will be piped input). One or&lt;br /&gt;
more filenames can be appended to the command line if the input does&lt;br /&gt;
not come from stdin. Output is sent to stdout (the screen). Thus:&lt;br /&gt;
&lt;br /&gt;
 cat filename | sed &amp;#039;10q&amp;#039;        # uses piped input&lt;br /&gt;
 sed &amp;#039;10q&amp;#039; filename              # same effect, avoids a useless &amp;quot;cat&amp;quot;&lt;br /&gt;
 sed &amp;#039;10q&amp;#039; filename &amp;gt; newfile    # redirects output to disk&lt;br /&gt;
&lt;br /&gt;
For additional syntax instructions, including the way to apply editing&lt;br /&gt;
commands from a disk file instead of the command line, consult &amp;quot;sed &amp;amp;&lt;br /&gt;
awk, 2nd Edition,&amp;quot; by Dale Dougherty and Arnold Robbins (O&amp;#039;Reilly,&lt;br /&gt;
1997; http://www.ora.com), &amp;quot;UNIX Text Processing,&amp;quot; by Dale Dougherty&lt;br /&gt;
and Tim O&amp;#039;Reilly (Hayden Books, 1987) or the tutorials by Mike Arst&lt;br /&gt;
distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power&lt;br /&gt;
of sed, one must understand &amp;quot;regular expressions.&amp;quot; For this, see&lt;br /&gt;
&amp;quot;Mastering Regular Expressions&amp;quot; by Jeffrey Friedl (O&amp;#039;Reilly, 1997).&lt;br /&gt;
The manual (&amp;quot;man&amp;quot;) pages on Unix systems may be helpful (try &amp;quot;man&lt;br /&gt;
sed&amp;quot;, &amp;quot;man regexp&amp;quot;, or the subsection on regular expressions in &amp;quot;man&lt;br /&gt;
ed&amp;quot;), but man pages are notoriously difficult. They are not written to&lt;br /&gt;
teach sed use or regexps to first-time users, but as a reference text&lt;br /&gt;
for those already acquainted with these tools.&lt;br /&gt;
&lt;br /&gt;
QUOTING SYNTAX: The preceding examples use single quotes (&amp;#039;...&amp;#039;)&lt;br /&gt;
instead of double quotes (&amp;quot;...&amp;quot;) to enclose editing commands, since&lt;br /&gt;
sed is typically used on a Unix platform. Single quotes prevent the&lt;br /&gt;
Unix shell from intrepreting the dollar sign ($) and backquotes&lt;br /&gt;
(`...`), which are expanded by the shell if they are enclosed in&lt;br /&gt;
double quotes. Users of the &amp;quot;csh&amp;quot; shell and derivatives will also need&lt;br /&gt;
to quote the exclamation mark (!) with the backslash (i.e., \!) to&lt;br /&gt;
properly run the examples listed above, even within single quotes.&lt;br /&gt;
Versions of sed written for DOS invariably require double quotes&lt;br /&gt;
(&amp;quot;...&amp;quot;) instead of single quotes to enclose editing commands.&lt;br /&gt;
&lt;br /&gt;
USE OF &amp;#039;\t&amp;#039; IN SED SCRIPTS: For clarity in documentation, we have used&lt;br /&gt;
the expression &amp;#039;\t&amp;#039; to indicate a tab character (0x09) in the scripts.&lt;br /&gt;
However, most versions of sed do not recognize the &amp;#039;\t&amp;#039; abbreviation,&lt;br /&gt;
so when typing these scripts from the command line, you should press&lt;br /&gt;
the TAB key instead. &amp;#039;\t&amp;#039; is supported as a regular expression&lt;br /&gt;
metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.&lt;br /&gt;
&lt;br /&gt;
VERSIONS OF SED: Versions of sed do differ, and some slight syntax&lt;br /&gt;
variation is to be expected. In particular, most do not support the&lt;br /&gt;
use of labels (:name) or branch instructions (b,t) within editing&lt;br /&gt;
commands, except at the end of those commands. We have used the syntax&lt;br /&gt;
which will be portable to most users of sed, even though the popular&lt;br /&gt;
GNU versions of sed allow a more succinct syntax. When the reader sees&lt;br /&gt;
a fairly long command such as this:&lt;br /&gt;
&lt;br /&gt;
   sed -e &amp;#039;/AAA/b&amp;#039; -e &amp;#039;/BBB/b&amp;#039; -e &amp;#039;/CCC/b&amp;#039; -e d&lt;br /&gt;
&lt;br /&gt;
it is heartening to know that GNU sed will let you reduce it to:&lt;br /&gt;
&lt;br /&gt;
   sed &amp;#039;/AAA/b;/BBB/b;/CCC/b;d&amp;#039;      # or even&lt;br /&gt;
   sed &amp;#039;/AAA\|BBB\|CCC/b;d&amp;#039;&lt;br /&gt;
&lt;br /&gt;
In addition, remember that while many versions of sed accept a command&lt;br /&gt;
like &amp;quot;/one/ s/RE1/RE2/&amp;quot;, some do NOT allow &amp;quot;/one/! s/RE1/RE2/&amp;quot;, which&lt;br /&gt;
contains space before the &amp;#039;s&amp;#039;. Omit the space when typing the command.&lt;br /&gt;
&lt;br /&gt;
OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to&lt;br /&gt;
large input files or slow processors or hard disks), substitution will&lt;br /&gt;
be executed more quickly if the &amp;quot;find&amp;quot; expression is specified before&lt;br /&gt;
giving the &amp;quot;s/.../.../&amp;quot; instruction. Thus:&lt;br /&gt;
&lt;br /&gt;
   sed &amp;#039;s/foo/bar/g&amp;#039; filename         # standard replace command&lt;br /&gt;
   sed &amp;#039;/foo/ s/foo/bar/g&amp;#039; filename   # executes more quickly&lt;br /&gt;
   sed &amp;#039;/foo/ s//bar/g&amp;#039; filename      # shorthand sed syntax&lt;br /&gt;
&lt;br /&gt;
On line selection or deletion in which you only need to output lines&lt;br /&gt;
from the first part of the file, a &amp;quot;quit&amp;quot; command (q) in the script&lt;br /&gt;
will drastically reduce processing time for large files. Thus:&lt;br /&gt;
&lt;br /&gt;
   sed -n &amp;#039;45,50p&amp;#039; filename           # print line nos. 45-50 of a file&lt;br /&gt;
   sed -n &amp;#039;51q;45,50p&amp;#039; filename       # same, but executes much faster&lt;br /&gt;
&lt;br /&gt;
If you have any additional scripts to contribute or if you find errors&lt;br /&gt;
in this document, please send e-mail to the compiler. Indicate the&lt;br /&gt;
version of sed you used, the operating system it was compiled for, and&lt;br /&gt;
the nature of the problem. To qualify as a one-liner, the command line&lt;br /&gt;
must be 65 characters or less. Various scripts in this file have been&lt;br /&gt;
written or contributed by:&lt;br /&gt;
&lt;br /&gt;
 Al Aab                   # founder of &amp;quot;seders&amp;quot; list&lt;br /&gt;
 Edgar Allen              # various&lt;br /&gt;
 Yiorgos Adamopoulos      # various&lt;br /&gt;
 Dale Dougherty           # author of &amp;quot;sed &amp;amp; awk&amp;quot;&lt;br /&gt;
 Carlos Duarte            # author of &amp;quot;do it with sed&amp;quot;&lt;br /&gt;
 Eric Pement              # author of this document&lt;br /&gt;
 Ken Pizzini              # author of GNU sed v3.02&lt;br /&gt;
 S.G. Ravenhall           # great de-html script&lt;br /&gt;
 Greg Ubben               # many contributions &amp;amp; much help&lt;br /&gt;
-------------------------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Adk44</name></author>
	</entry>
</feed>