SED -- STREAM EDITOR
sed 'command;command;command' somefile see results
sed 'command;command;command' somefile > newfile save results
.... | sed 'command;command;command' | .... use in a pipeline
sed -f sedcommands somefile > newfile commands are in file
somewhere else
Commands:
s -- substitute strings
y -- translate characters
d -- delete lines
!d -- extract (keep) lines
p -- print lines
MOST COMMON PATTERN: substitute every occurrence of a string "old" with "new"
sed 's/old/new/g' somefile > newfile
The patterns in these commands use regular expressions for pattern matching:
* 0 or more occurrences of previous character
. any character except newline
[...] any single occurrence of character in set
[a-z] any single occurrence of character in range
[^...] any single occurrence of character NOT in set (or range)
^ beginning of line
$ end of line
\ escapes the following character
\{n,m\} range of occurrences, n and m are integers
+ one or more occurrence of previous character
? 0 or more occurrences of preceding regular expression
| match either r.e. on left or r.e. on right
() groups regular expressions
EXAMPLES:
/^M.*/ Line begins with capital M, 0 or more chars follow
/..*/ At least 1 character long
/^$/ The empty line
ab|cd Either ab or cd
Examples:
sed 's/^Unix/UNIX(TM)/' ... The caret (^) matches the beginning of a
line, so this only changes Unix if it is at
the beginning of a line
sed 's/:$//' ... The dollar sign ($) matches the end of a
line, so this removes a colon only if it is
at the end of a line.
More details in the sed examples: