Stream Editor sed Overview and Reference
Mai 6, 2015
Views: 1916
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 N th 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 s ubstitution since last line was read or since last conditional branch |
T LABEL | branch to label only if there was no successful s ubstitution (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
|
y/SOURCE-CHARS/DEST-CHARS/ | replace character by character, such as in tr , instead of / you can use other characters |