More Complex Sed
Compliments of Dr. Dailey
Command Description
$ cat simple
Here is a file containing some words.
A total of 3 lines have been used.
This line makes the last one right.
A file.
$ sed -n /A/,3p simple
A total of 3 lines have been used.
This line makes the last one right.
Printing from the first line that contains 'A' through line number 3 of the file.
$wc -l simple

3 simple ---------------------------------------- $c=3 $sed -n /A/,"$c"p simple A total of 3 lines have been used. This line makes the last one right.
Suppose we wished to print from /A/ through the end of the file, but didn't know how many lines there were. We can use wc -l to tell us how many lines, but then suppose we wish to use that output in our sed statement. The output of the wc -l command is non-numeric and cannot directly be used. So how might we use sed to transform the output of wc -l to just the numeric part?
Sample
$c=`wc -l simple|sed s/.simple//`
$echo $c
3
$ sed -n /A/,"$c"p simple
A total of 3 lines have been used.
This line makes the last one right.
We define $c as the number of lines in the file (by catching the output of wc and removing everything after the whitespace). Then we print every line of simple starting with the first line which contains uppercase 'A' until the end of the file.
$wc -l simple|sed "s/\([^' ']\)\ *[^' ']*/\1/"
3
If we wanted to generalize the above so it works with any file and not just 'simple', then we could look at any blanks which occur between strings of nonblanks and delete from there to the end of the line by remembering, and later recalling, the preceding non-blanks.
$ wc -l simple|sed "s/\ \ *[^' ']*//2"
      3
$c=`wc -l simple|sed "s/\ \ *[^' ']*//2"`
[ddailey]$sed -n /A/,"$c"p simple
A total of 3 lines have been used.
This line makes the last one right.
Another (perhaps simpler?) approach would be to second occurence of non-blanks following blanks and simply delete those.
$ sed y/aeiou/iouae/ simple
Horo us i fulo cantiunung samo wards.
A tatil af 3 lunos hivo boon esod.
Thus luno mikos tho list ano rught.
Transform occurrences of a to i; e to o; i to u; o to a; and u to e throughout the file.
$ sed s/\ .e/\&*/g simple
Here is a file containing some words.
A total of 3 lines have be*en used.
This line makes the last one right.  
Whenever e occurs as the second character within a word (something preceded by a blank), append an asterisk to it. [Note: '\ ' matches the space character, while \& refers to whatever pattern was found on the left.]
sed s/\ .e/*/g simple
Here is a file containing some words.
A total of 3 lines have*en used.
This line makes the last one right.
The same as above, but without the recall (through '&') of the matched string.
$ sed s/.t/\&\&/g simple
Here is a file contntaining some words.
A t tototal of 3 lines have been used.
This line makes t the lastst one rightht.
Whenever t occurs, rewrite it and the character which precedes it twice.
$ cat f4
audiogalaxy.jpg::pic1.jpg
weather.jpg::pic2.gif
dialpad.jpg::wp156.htm
scour.jpg::wp156.htm
dailey.jpg::Royas11cpsc130sect2.html
mellon.jpg::pic1.jpg
hotmail.jpg::130.2assignment11.html
napster.jpg::j1.gif
Here's a file in which each line has material to the left and right of two colons.
$ sed 's/\(.*\)::\(.*\)/\2**\1/' f4
pic1.jpg**audiogalaxy.jpg
pic2.gif**weather.jpg
wp156.htm**dialpad.jpg
wp156.htm**scour.jpg
Royas11cpsc130sect2.html**dailey.jpg
pic1.jpg**mellon.jpg
130.2assignment11.html**hotmail.jpg
j1.gif**napster.jpg        
We switch the order of the material, by remembering strings. (string) remembers a string in the pattern space. The first of these can later be recalled as \1 (or \n for the nth remembered string). Since '(' is a metacharacter, we have to use '\(' to refer to it.
For fun we replace '::' by '**'.
$head -5 common5
abide
abode
about
above
abuse
$sed '4s/.*/ha!/g' common5|head -5
abide
abode
about
ha!
abuse
Replacing the characters in the 4th line of a file by a particular string.
$sed -n 1~2p common5|head -5
abide
about
abuse
acrid
acute 
Printing every other line of a file.(m~n means print every nth line starting at line m).

Multi-line replacements

Command Description
$cat simple
Here is a file containing some words.
A total of 3 lines have been used.
This line makes the last one right.

$sed 's/\ /\ > /g' simple|head -7
Here
is
a
file
containing
some
words.
Putting each word of a file on a separate line:
We substitute the literal character 'space' by the literal
character 'newline' (signalled by actually typing a carriage return).  By putting the substitute part of the sed command inside single  quotes, allows the command to straddle more than a single line
and hence for the carriage return to be seen as a literal character,
rather than an end-of-command mark.
$cat simple
Here is a file containing some words.
A total of 3 lines have been used.
This line makes the last one right.

$sed 'N;s/\n/ /' simple
Here is a file containing some words. A total of 3 lines have been used.
This line makes the last one right.
In order to insert the newline character (matched in the pattern
space as \n), one has to look across multiple lines, by using 
the command N -- meaning load next line, as well as the current,
into the pattern space. sed 's/\n/ /' simple won't do it.
cat alph10
a
b
c
d
e
f
g
h
i
j

$ sed 'N;N;N;N;s/\n//g' alph10
abcde
fghij
reapply the above principle several times, placing  five lines into a single buffer, prior to the replacement of the newline character.
Sample