4.8. 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 white space 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. In fact, you cannot use quotes in a sed script at all, unless they are part of a search pattern.

% 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
				

4.8.1. sed Script Examples

Example 4.35.
% cat sedding1
						(Look at the contents of the sed script.)
1 # My first sed script by Jack Sprat2
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; the
						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 4.36.
% 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, using the Unix version of sed, 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/

% 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.

Example 4.37.
1 % cat bye4now
						(Display the contents of the sed script.)
   # The sed script
   $a
       Thankyou for coming
               bye

2 % sed -e 's/Charles/Jimmy/' -f bye4now datafile
						northwest    NW
						Jimmy
						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 15
						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

       Thankyou for coming
               bye

3 % sed --file=bye4now --expression='s/Charles/Jimmy/' datafile
						northwest    NW
						   Jimmy
						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  15
						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

       Thankyou for coming
               bye

Explanation

  1. The contents of a sed script called bye4now are displayed. $a means: Append the following text after the last line of the input file.

  2. The -e option is used to evaluate the sed substitution command, i.e., substitute Charles with Jimmy. The -f option is used to read commands from the sed script bye4now.

  3. The long format of the -f option, --file, is assigned the name of the sed script, followed by the long form of the -e option, --expression, and the sed substitution command.

4.8.2. sed Review

Table 4.4 lists sed commands and what they do.

Table 4.4. sed Review
CommandWhat It Does
sed -n '/sentimental/p' filexPrints 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 > newfilexDeletes lines 1, 2, and 3 from filex and saves changes in newfilex.
sed '/[Dd]aniel/d' filexDeletes lines containing Daniel or daniel.
sed -n '15,20p' filexPrints only lines 15 through 20.
sed`1, 10s/Montana/MT/g' filexSubstitutes Montana with MT globally in lines 1 through 10.
sed `/March/!d' filex (tcsh)sed '/March/!d' filex (sh)Deletes all lines not containing March. (The backslash is used in tcsh to escape the history character.)
sed '/report/s/5/8/' filexChanges the first occurrence of 5 to 8 on all lines containing report.
sed 's/....//' filexDeletes the first four characters of each line; i.e., substitutes the first four characters with nothing.
sed 's/…$//' filexDeletes the last three characters of each line; i.e., substitutes the last three characters with nothing.
sed '/east/,/west/s/North/South/' filexFor any lines falling in the range from east to west, substitutes North with South.
sed -n '/Time off/w timefile' filexWrites all lines containing Time off to the file timefile.
sed 's/([Oo]ccur)ence/1rence/' fileSubstitutes either Occurence or occurence with Occurrence or occurrence.
sed -n 'l' filexPrints all lines showing nonprinting characters as n where nn is the octal value of the character, and showing tabs as >.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.171.212