Previous Section  < Day Day Up >  Next Section

5.6. Commands and Options

Sed commands tell sed how to process each line of input specified by an address. If an address is not given, sed processes every line of input. (The % is the csh prompt.) See Table 5.1 for a list of sed commands and what they do, and see Table 5.2 for a list of options and how they control sed's behavior.

Table 5.1. sed Commands

Command

Function

a\

Appends one or more lines of text to the current line

c\

Changes (replaces) text in the current line with new text

d

Deletes lines

i\

Inserts text above the current line

h

Copies the contents of the pattern space to a holding buffer

H

Appends the contents of the pattern space to a holding buffer

g

Gets what is in the holding buffer and copies it into the pattern buffer, overwriting what was there

G

Gets what is in the holding buffer and copies it into the pattern buffer, appending to what was there

l

Lists nonprinting characters

p

Prints lines

n

Reads the next input line and starts processing the newline with the next command rather than the first command

q

Quits or exits sed

r

Reads lines from a file

!

Applies the command to all lines except the selected ones

s

Substitutes one string for another

Substitution Flags

g

Globally substitutes on a line

p

Prints lines

w

Writes lines out to a file

x

Exchanges contents of the holding buffer with the pattern space

y

Translates one character to another (cannot use regular expression metacharacters with y)


Table 5.2. sed Options

Options

Function

–e

Allows multiple edits

–f

Precedes a sed script filename

–n

Suppresses default output


When multiple commands are used or addresses need to be nested within a range of addresses, the commands are enclosed in curly braces and each command is either on a separate line or terminated with semicolons.

The exclamation point (!) can be used to negate a command. For example,


sed '/Tom/d' file


tells sed to delete all lines containing the pattern Tom, whereas


sed '/Tom/!d' file  (sh, ksh, bash)

sed '/Tom/\!d' file  (csh, tcsh)


tells sed to delete lines not containing Tom.

The sed options are –e, –f, and, –n. The –e is used for multiple edits at the command line, the –f precedes a sed script filename, and the –n suppresses printing output.

5.6.1 How to Modify a File with sed

Sed is a nondestructive editor. It will display the edits you make on your screen, but it will not change the file you are editing. To really reflect the edits in the file, you must redirect the output to another file, and then rename the orginal file.

Example 5.5.

1   % sed '1,3d' filex >  temp

2   % mv temp filex


EXPLANATION

  1. Lines 1 through 3 of myfile are deleted. Instead of displaying the remaining lines on the screen, they are redirected to a file called temp. (Make sure the file where you are sending your output, in this case temp, is an empty file. Otherwise the redirection will cause it to be clobbered.)

  2. The mv command will overwrite filex with the contents of temp.

5.6.2 GNU sed Options

Example 5.6 lists additional options provided by GNU sed, and how they control sed's behavior. With the –h option, sed displays a list of its command-line options and a short description of what each one does.

Example 5.6.

% sed -h

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...



    -n, --quiet, --silent

                  suppress automatic printing of pattern space

    -e script, --expression=script

                  add the script to the commands to be executed

    -f script-file, --file=script-file

                  add the contents of script-file to the commands to be executed

    --help        display this help and exit

    -V, --version output version information and exit


EXPLANATION

If an –e, – –expression, –f, or – –file option is not given, then the first nonoption argument is taken as a sed script to be interpreted. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.

    Previous Section  < Day Day Up >  Next Section