Skip to main content

sed

Ref

CLI Option

sed [<option>] '<script>' <input file> <input file> ...
OptionDescription
-i[<suffix>]Edit files in place (makes backup if <suffix> supplied)
-rExtended regular expressions

<script>

[<address>]<command> [; [<address>]<command> ...]

hold spaceclipboard

pattern spacecurrent record

Address

AddressDescription
<number>Line number
$Last line
<first>,<last>Line number range
<first>~<step>Line number step
/<RegExp>/

\<character><RegExp><character>
RegExp
<address>!Not matching <address>

Command

CommandDescription
dDelete and go to next cycle
s/<RegExp>/<replacement>/[<flags]>Substitute RegExp
=Print line number
hReplace hold space with pattern space
HAppend hold space to pattern space
gReplace pattern space with hold space
GAppend pattern space to hold space
q[<exit code>]Quit with exit code
{ <command>; <command>; ... }Group command

RegExp flags

FlagDescription
gGlobal, replace all matches
<number>Replace the number th match
pPrint if match
iCase insensitive
mMulti-line mode

RegExp capture group

ExpressionDescription
&Whole matched string
\1 \2 ...Capture group