15 sed stream editor Flashcards
sed scripts can be provided on the command line
sed [-e] ‘command’ files
All commands are applied in sequence to each line
sed scripts can be provided in a separate file
sed -f scriptfile files
General form of a sed command:
[address[,address]][!]command[arguments]
Addresses
Addresses can be line numbers or regular expressions.
Last line
Last line is “$”.
Line range
One address selects a line, two addresses a line range (specifying start and end line).
Optionally print
After this, the line is printed, unless option -n is used, in which case only the p command will print a line.
Negate address matches
The ! negates address match. {. . . } can group commands per address.
Regex enclosed in
Regular expressions enclosed in /. . . /. Some regular expression meta characters:
matches any character (except new-line)
“.”
matches the preceding item zero or more times
“*”
matches the preceding item one or more times
“+”
matches the preceding item optionally (0–1 times)
“?”
matches start of line
“^”
matches end of line
“$”
matches one of listed characters
use in character list “^” to negate and “-” for ranges
“[. . . ]”
grouping, “{n,m}” match n, . . . , m times
“(. . . )”
escape following meta character
“\”
Substitute all occurrences of “Windows” with “Linux” (command: s = substitute, option: g = “global” = all occurrences in line):
sed ‘s/Windows/Linux/g’
[used within a pipeline]
Delete all lines that do not end with “OK” (command: d = delete):
sed ‘/OK$/!d’
Print only lines between those starting with BEGIN and END, inclusive:
sed -n ‘/^BEGIN/,/^END/p’
Substitute in lines 40–60 the first word starting with a capital letter with “X”:
sed ‘40,60s/[A-Z][a-zA-Z]*/X/’