4.14 awk - A Programming Language4 Labs4.12 bc - Extended Precision Calculator4.13 sed -- A Stream Editor

4.13 sed -- A Stream Editor

sed is a text editor. But, it is NOT an interactive text editor. You need to decide exactly what and how you are going to edit prior to getting started. If you have multiple files that need the same changes it is a good choice. If you only have one file and the changes are complex, find another editor -- sed is not your answer.

sed works by reading one input line into a buffer, referred to as "pattern space", processing all of the commands related to that line, normally printing the resulting line, then reading the next line

In addition to the standard output, results can be directed to several files. Additional input can be read from files and from the program.

In addition to the "pattern space" there is a "hold space".

Invoking sed:

sed [-n] [-e script] [-f sfile] [file...]

-n is used to suppress the printing of lines after processing, normally used in conjunction with the p (print) command.

-e is used to indicate that the following argument is the program. This is normally optional.

-f is used to indicate the file that contains the text processing program.

The input to sed, by default, comes from the standard input. If a file name(s) is provided the input comes from the file(s).

Output from sed goes to the standard output. It does not modify the input file(s).

Addresses and Commands:

[address [, address] ] command [arguments]

An address is either a line number, a "$" (the last line), or a context address, "/regular expression/", in the style of ed.

1,$            All lines 
32             Line 32 
/Some/         lines with the string  Some 
/Start/,/End/  Lines, inclusive, starting with a 
               line containing  Start and ending  
               with a line containing  End 

Commands are one character following an optional address.

 
s/regular expression/replacement/flags

If the replacement contains an "&" it represents the matched regular expression. flags g -- global; p -- print after replacement; wwfile - write to file wfile if replacement was made.

p     Print the current "pattern space".

q     Quit processing.

d     Delete the pattern space.  Do not print.

a\    Append text to output prior to reading next line. 
text 

i\    Insert text to output. 
text

y/abcd/ABCD/ 
             Transform any matching character in the 
             first string to the corresponding 
             character in the second string.  Strings 
             must be same length.

Write a sed script/program that changes:

~cis137/sed.text from: Also avaliable at:
http://uml.lt.tucson.az.us/hl2.2007-fall/files/sed.text
.

Tkis is the input to the sed lab. 
It is in a file in ~cis137/sed.text on system gort 
Tkis line hax the nmbers 123456789 and 0 
Tkis line hax the nmbers 154374830 and 2 
Delete this line.
And tkis line too.

to:

This is the input to the sed lab. 
It is in a file in ~cis137/sed.text on system lt.tucson.az.us
This line has the numbers 234567890 and 1
This line has the numbers 265485941 and 3 
And this line too.

Turn in this script program marked with:

 
your-name
TABER CIS137
Lab 4.13: sed program 

Instructor: Louis Taber, louis.taber.at.pima at gmail dot com (520) 206-6850
My web site in Cleveland, OH
The Pima Community College web site


4.14 awk - A Programming Language4 Labs4.12 bc - Extended Precision Calculator4.13 sed -- A Stream Editor