Marc Wäckerlin
Für eine libertäre Gesellschaft

Stream Editor sed Overview and Reference

Mai 6, 2015

Views: 1914

I was missing a one-page overwiew of sed commands, so here they are:

Command Line Options

option description
–version show version
–help show short usage
-n
–quiet
–silent
only produces output when explicitly told to via the p command
-e SCRIPT
–expression=SCRIPT
explicitly add script expression
-f SCRIPT-FILE
–file=SCRIPT-FILE
read script from file
-i[SUFFIX]
–in-place[=SUFFIX]
in place edit file, if suffix is given, creates a backup
-l N
–line-length=N
default line length for the l command
–posix strict posix mode
-b
–binary
binary with regard to CR/LF problem
–follow-symlinks follow links when option -i is used
-r
–regexp-extended
use extended regular expression syntax (GNU extension)
-s
–separate
treat each input file separately instead of one long stream (GNU extension)
-u
–unbuffered
buffer as little as possible
-z
–null-data
–zero-terminated
separate lines with zero instead of newline

Adresses

addressing description
(no address) match all lines
NUMBER select line number
FIRST~STEP matches every STEPth line starting with line FIRST
$ match last line
/REGEXP/ match regular expression
// repeat last regular expression match
\%REGEXP% same as /REGEXP/, but with % as delimiter – also works for other characters
/REGEXP/I match case insensitive (GNU extension)
/REGEXP/M match in multi line mode, then ^ $ match begin and end of a line, \` \' match begin and end of buffer (GNU extension)
ADRESS1,ADDRESS2 match address range from to, range will always span at least two lines, unless the second address is a number less than (or equal to) the line number matching the first address
0,ADDRESS2 apply ADDRESS2 already on the first line (GNU extension)
ADDRESS1,+N matches for the following N lines (GNU extension)
ADDRESS1,-N matches until the next line whose input line number is a multiple of N
ADDRESS! negate the sense of the match

Regular Expressions

regexp description
C matches any given character C
. matches any character
* repeat last match zero or more times
\+ repeat last match one or more times
\? repeat last match zero or one times
\{I\} repeat last match exactly I times (GNU extension)
\{I,J\} repeat last match between I and J times (GNU extension)
\{I,\} repeat last match at least I times (GNU extension)
\(REGEXP\) group – can be refered as \N for the Nth group
^ matches begin of line
$ matches end of line
[LIST] matches any single character in list
[^LIST] matches any single character except those in list
RE1\|RE2 or
\C escape special character C, such as $ * . [ \ ^
\w matches any «word» character (GNU extension)
\W matches any «non-word» character (GNU extension)
\b matches a word boundary (GNU extension)
\B matches everywhere but on a word boundary (GNU extension)
\` matches only at the start of pattern space (file) (GNU extension)
\› matches only at the end of pattern space (file) (GNU extension)
\s matches a white space character

Escapes

char description
\a produces or matches a BEL character (GNU extension)
\f produces or matches a form feed (GNU extension)
\n produces or matches a newline (GNU extension)
\r produces or matches a carriage return (GNU extension)
\t produces or matches a horizontal tab (GNU extension)
\v produces or matches a vertical tab (GNU extension)
\cX produces or matches CONTROL-X (GNU extension)
\dXXX produces or matches a character whose decimal ASCII value is XXX (GNU extension)
\oXXX produces or matches a character whose octal ASCII value is XXX (GNU extension)
\xXX produces or matches a character whose hexadecimal ASCII value is XX (GNU extension)

Commands

command description
# comment line (no address)
q [EXIT-CODE] exit with optional exit code
Q [EXIT-CODE] quit without printing current line (GNU extension)
d delete pattern space, start next cycle
D delete up to first newline, then restart without reading a new line of input
p print pattern space
P print up to first newline
n proceed to next line
N append new line plus next line from input
a\
TEXT
append text after (GNU extension: this command accepts two addresses)
aTEXT\
OPTIONAL-MORE-TEXT
append text (simplified GNU extension)
i\
TEXT
insert text before (otherwise same as append text after)
c\
TEXT
replace text (otherwise same as append text after)
= print input line number (GNU extension: this command accepts two addresses)
l N print in C style printable format, wrap long lines at N characters (GNU extension: N)
L N joins lines in pattern space to produce output lines of (at most) N characters – deprecated (GNU extension)
r FILENAME append a file (GNU extension: this command accepts two addresses)
R FILENAME append only a single line of a file (GNU extension)
w FILENAME write to a file, or append if the same file name is given several times
W FILENAME write up to first newline (GNU extension)
h copy to buffer
H append to buffer with preseeding new line
g copy from buffer
G append newline plus content of buffer
x exchange current line with buffer
: LABEL label to branch to
b LABEL branch to label
t LABEL branch to label if there was a successful substitution since last line was read or since last conditional branch
T LABEL branch to label only if there was no successful substitution (GNU extension)
e COMMAND pipe input from shell command (GNU extension)
e pipe input from command in current line (GNU extension)
F print file name of current input (GNU extension)
v VERSION fail if current sed’s version is lower (GNU extension)
z drop whole content of current line (GNU extension)
{ COMMANDS } group commands
s/REGEXP/REPLACEMENT/FLAGS search replace, instead of / you can use other characters

REGEXP
match regular expression
REPLACEMENT
replace with REPLACEMENT, where:

\N
refers the the Nth match
&
referes the the whole line
\L
to turn the replacement to lowercase until a \U or \E is found
\l
turn the next character to lowercase
\U
turn the replacement to uppercase until a \L or \E is found
\u
turn the next character to uppercase
\E
dtop case conversion started by \L or \U
FLAGS
optional flags which are

g
apply to all matches, nut just the first
N
only replace Nth match
p
print
w FILENAME
write to file
e
execute match as shell command and replace with output (GNU extension)
i
I
case insensitive (GNU extension)
m
M
match in multi line mode, then ^ $ match begin and end of a line, \` \' match begin and end of buffer (GNU extension)
y/SOURCE-CHARS/DEST-CHARS/ replace character by character, such as in tr, instead of / you can use other characters

comments title