Stream Editing
sed
Batch editor may be used as a filter in one of 2 ways:
sed [ -n ] -f script-file [ file-list ]
sed [ -n ] script [ file-list ]
- Edits every line of every file in file-list, or standard input if no
file-list is specified
- Editing instructions are contained in a script
- script-file may contain several lines of instructions, executed in
sequence
- script is a single argument, usually enclosed in ''
- Unless -n option is used, every line examined/edited by sed is written
to standard output
sed scripts
A script contains one or more lines with the format:
[addr[, addr]] instruction [arg-list]
Lines may be separated with ;
The sed utility processes input as follows:
- Copy one line from the input (minus terminating NEWLINE character)
into a pattern space,
- Apply in sequence all commands whose addresses select that pattern
space,
- Copy the resulting pattern space to the standard output (except under
-n), and
- Delete the pattern space.
Whenever the pattern space is written to standard output or a named
file, sed will immediately follow it with a NEWLINE character.
Editing Commands
Different commands require different sets of arguments.
| a\ text |
append lines (until finding one not ending in '\') after outputting
the pattern space |
| b |
label branch to command : label |
| c\ text |
change selected line(s) to following text, as in a |
| d |
delete line, then read next input line |
| i\ text |
insert lines (as in a) before outputting the pattern space |
| l |
list, showing all non-printing characters |
| p |
print line |
| q |
quit |
| r file |
read file, copy contents to output |
| s/old/new/f |
substitute new for old; if f=g, replace all occurrences; if f=p, print;
if f=w file, write to file |
Editing Commands (continued)
| t label |
test: branch to label if substitution made to current line |
| w file |
write line to file |
| y/str1/str2/ |
replace each character from str1 with corresponding character from
str2 |
| = |
print current input line number |
| !cmd |
do the sed command only if the current line is not selected |
| : label |
set label for b and t commands |
| { |
treat commands up to matching } as a group |
Addresses
Lines may be addressed explicitly or using regular expressions
- May specify 0, 1, or 2 addresses
- If no address, applies to all input lines
- With 1 address, applies to a single input line
- With 2 addresses, applies to the range of lines
- Explicit addresses are numbered
- begin with 1 (the 1st line of the 1st file in file-list)
- end with $ (the last line of the last file in file-list)
- Regular expressions match patterns in the line
- a delimiter character (typically '/') marks the beginning and end of
a regular expression
- if the regular expression matches any part of the line, the entire
line is said to match
Regular Expressions
Represent patterns to be replaced (for s and y commands) or identifying
target input lines (for addresses)
- . matches any character
- [] defines a character class that matches any single character within
the brackets
- * matches 0 or more occurrences of the regular expression immediately
to the left
- ^ matches the beginning of the line
- $ matches the end of the line
- \n represents the newline character
- in a replacement string, & represents the pattern that was matched
- all other characters represent themselves (case sensitive)
- special characters must be quoted if they are to be interpreted literally