Console9

sed commands and flags reference

Complete reference for sed commands (s, d, p, i, a), flags (-i, -n, -e), and address ranges.

sed commands and flags reference

Complete reference for sed commands (s, d, p, i, a), flags (-i, -n, -e), and address ranges.

sed Command-Line Flags

FlagDescriptionExample
-i [EXT]Edit files in place. GNU: -i (no backup) or -i.bak. BSD: -i '' or -i .bak.sed -i 's/old/new/g' file
-nSuppress default output. Print only lines selected by p command.sed -n '5,10p' file
-e SCRIPTAdd a script (command) to execute. Allows multiple commands.sed -e 's/a/b/' -e 's/c/d/' file
-E or -rUse extended regular expressions (no backslash escaping for groups).sed -E 's/(foo)/\1bar/' file
-f FILERead sed commands from a file.sed -f script.sed file

sed Editing Commands

CommandDescriptionExample
s/pat/repl/flagsSubstitute — replace pat with repl. g flag replaces all matches per line.s/old/new/g
dDelete the matched line(s)./^#/d deletes comment lines
pPrint the matched line(s). Use with -n to print only matching lines.-n '/error/p'
i TEXTInsert TEXT before the matched line./pattern/i New line above
a TEXTAppend TEXT after the matched line./pattern/a New line below
c TEXTReplace the matched line with TEXT./old line/c new line
y/SRC/DST/Transliterate characters (like tr).y/abc/ABC/
qQuit sed after processing the current line.10q quits after line 10

sed Address Ranges

AddressDescriptionExample
NLine number N.3d deletes line 3
$Last line.$d deletes the last line
N,MLines N through M.10,20s/old/new/
/regex/Lines matching the regex./error/d
N,/regex/From line N to the next line matching regex.1,/END/d
/start/,/end/From line matching start to line matching end./BEGIN/,/END/p

sed Substitution Flags

FlagDescriptionExample
gReplace all matches on the line (not just the first).s/old/new/g
NReplace only the Nth match.s/old/new/2 replaces the 2nd match
pPrint the line if a substitution was made.s/old/new/p
w FILEWrite the line to FILE if a substitution was made.s/old/new/w matches.txt
ICase-insensitive matching (GNU sed only).s/error/ERROR/Ig