Previous Section  < Day Day Up >  Next Section

5.10. sed Scripting

A sed script is a list of sed commands in a file. To let sed know your commands are in a file, when invoking sed at the command line, use the –f option followed by the name of the sed script. Sed is very particular about the way you type lines into the script. There cannot be any trailing whitespace or text at the end of the command. If commands are not placed on a line by themselves, they must be terminated with a semicolon. A line from the input file is copied into the pattern buffer, and all commands in the sed script are executed on that line. After the line has been processed, the next line from the input file is placed in the pattern buffer, and all commands in the script are executed on that line. Sed gets "garbled" if your syntax is incorrect.

The nice thing about sed scripts is that you don't have to worry about the shell's interaction as you do when at the command line. Quotes are not needed to protect sed commands from interpretation by the shell and only one backslash is used in line continuation. In fact, you cannot use quotes in a sed script at all, unless they are part of a search pattern.

The examples in this section use the following datafile.

% cat datafile

northwest

NW

Charles Main

3.0

.98

3

34

western

WE

Sharon Gray

5.3

.97

5

23

southwest

SW

Lewis Dalsass

2.7

.8

2

18

southern

SO

Suan Chin

5.1

.95

4

15

southeast

SE

Patricia Hemenway

4.0

.7

4

17

eastern

EA

TB Savage

4.4

.84

5

20

northeast

NE

AM Main Jr.

5.1

.94

3

13

north

NO

Margot Weber

4.5

.89

5

9

central

CT

Ann Stephens

5.7

.94

5

13


5.10.1 sed Script Examples

Example 5.38.

% cat sedding1                    # Look at the contents of the sed script

1   # My first sed script by Jack Sprat

2   /Lewis/a\

3       Lewis is the TOP Salesperson for April!!\

        Lewis is moving to the southern district next month.\

4       CONGRATULATIONS!

5   /Margot/c\

        *******************\

        MARGOT HAS RETIRED\

        ********************

6   1i\

    EMPLOYEE DATABASE\

    –––––––––––––––––––––

7   $d



% sed –f sedding1 datafile   # Execute the sed script commands; input file is datafile

EMPLOYEE DATABASE

––––––––––––––––––---

northwest             NW         Charles Main          3.0   .98     3     34

western               WE         Sharon Gray           5.3   .97     5     23

southwest             SW         Lewis Dalsass         2.7   .8      2     18

Lewis is the TOP Salesperson for April!!

Lewis is moving to the southern district next month.

CONGRATULATIONS!

southern              SO         Suan Chin             5.1   .95     4     15

southeast             SE         Patricia Hemenway     4.0   .7      4     17

eastern               EA         TB Savage             4.4   .84     5     20

northeast             NE         AM Main Jr.           5.1   .94     3     13



********************

MARGOT HAS RETIRED

*****************


EXPLANATION

  1. This line is a comment. Comments must be on lines by themselves and start with a pound sign (#).

  2. If a line contains the pattern Lewis, the next three lines are appended to that line.

  3. Each line being appended, except the last one, is terminated with a backslash. The backslash must be followed immediately with a newline. If there is any trailing text, even one space after the newline, sed will complain.

  4. The last line to be appended does not have the terminating backslash. This indicates to sed that this is the last line to be appended and that the next line is another command.

  5. Any lines containing the pattern Margot will be replaced (c command) with the next three lines of text.

  6. The next two lines will be inserted (i command) above line 1.

  7. The last line ($) will be deleted.

Example 5.39.

%  cat sedding2             # Look at the contents of the sed script

# This script demonstrates the use of curly braces to nest addresses

# and commands. Comments are preceded by a pound sign (#) and must

# be on a line by themselves. Commands are terminated with a newline

# or semicolon.  If there is any text after a command, even one

# space, you receive an error message:

#      sed: Extra text at end of command:



1   /western/, /southeast/{

        /^ *$/d

        /Suan/{ h; d; }

    }

2   /Ann/g

3   s/TB \(Savage\)/Thomas \1/

4 % sed -f sedding2 datafile

northwest             NW      Charles Main        3.0   .98    3   34

western               WE      Sharon Gray         5.3   .97    5   23

southwest             SW      Lewis Dalsass       2.7   .8     2   18

southeast             SE      Patricia Hemenway   4.0   .7     4   17

eastern               EA      Thomas Savage       4.4   .84    5   20

northeast             NE      AM Main Jr.         5.1   .94    3   13

north                 NO      Margot Weber        4.5   .89    5    9

southern              SO      Suan Chin           5.1   .95    4   15


EXPLANATION

  1. In the range of lines starting at western and ending at southeast, blank lines are deleted, and lines matching Suan are copied from the pattern buffer into the holding buffer, then deleted from the pattern buffer.

  2. When the pattern Ann is matched, the g command copies the line in the holding buffer to the pattern buffer, overwriting what is in the pattern buffer.

  3. All lines containing the pattern TB Savage are replaced with Thomas and the pattern that was tagged, Savage. In the search string, Savage is enclosed in escaped parentheses, tagging the enclosed string so that it can be used again. It is tag number 1, referenced by \1.

  4. Sed will get its commands from the file following the –f option, sedding2. The file that sed is editing is datafile.

5.10.2 sed Review

Table 5.4 lists sed commands and what they do.

Table 5.4. sed Review

Command

What It Does

sed –n '/sentimental/p' filex

Prints to the screen all lines containing sentimental.

The file filex does not change. Without the –n option, all lines with sentimental will be printed twice.

sed '1,3d' filex > newfilex

Deletes lines 1, 2, and 3 from filex and saves changes in newfilex.

sed '/[Dd]aniel/d' filex

Deletes lines containing Daniel or daniel.

sed –n '15,20p' filex

Prints only lines 15 through 20.

sed '1,10s/Montana/MT/g' filex

Substitutes Montana with MT globally in lines 1 through 10.

sed '/March/\!d' filex    (csh)
sed '/March/!d' filex     (sh)

Deletes all lines not containing March. (The backslash is used only in the csh to escape the history character.)

sed '/report/s/5/8/' filex

Changes the first occurrence of 5 to 8 on all lines containing report.

sed 's/....//' filex

Deletes the first four characters of each line.

sed 's/...$//' filex

Deletes the last three characters of each line.

sed '/east/,/west/s/North/South/' filex

For any lines falling in the range from east to west, substitutes North with South.

sed –n '/Time off/w timefile' filex

Writes all lines containing Time off to the file timefile.

sed 's/\([Oo]ccur\)ence/\1rence/' file

Substitutes either Occurence or occurence with Occurrence or occurrence.

sed –n 'l' filex

Prints all lines showing nonprinting characters as \nn, where nn is the octal value of the character, and showing tabs as >.


    Previous Section  < Day Day Up >  Next Section