general form
sed [range from](,[range to])?[command]


a       # match the letter a
\.      # match a literal dot '.'
\\      # match backslash
\t      # match tab
\n      # match newline
\r      # match carriage return
^       # match start of pat space
$       # match end of pat space

.       # match any character
[0-9]	# match digit
\s	# match whitespace
\w      # match [a-zA-Z0-9_] (word character)
\W      # match [^a-zA-Z0-9_] (non-word character)
\b      # match word boundary
\B      # match non-word boundary
\<      # match start of word
\>      # match end of word
\|      # logical or (alternative pattern)
*       # zero or more
\?      # zero or one
\+      # one or more
\{n\}   # exactly n matches
\{n,\}  # n or more matches
\{,n\}  # zero to n matches
\{n,m\} # n to m matches
\(      # open capturing group
\)      # close capturing group
\n      # refer to captured group [n]
&       # refer to complete pattern captured
$       # last line
n       # line number n
+n      # in upper bound: plus the next n lines (gnu extension)
n~m     # on line number n and re-execute every m lines after n
!       # inverse addresses
change case
\l      # next char to lower case
\u      # next char to upper case
\L      # next span to lower case
\U      # next span to upper case
\E	# end case changes for rest of span
special cases in character classes
[]]     # match closing bracket ']'
[^]]    # match anything but closing bracket
[-]     # match a literal dash '-'
[^-]    # match anything but a dash

command index

a = append
b = branch
c = change text
d = delete lines
D = delete first line (in patspace)
e = execute on the command line
g = copy from holdspace
G = append from hold space
h = copy to hold space
H = append to hold space
i = insert
l = print substitute line feeds
n = next input line
N = append next input line
p = print
P = print first line
q = quit
Q = quit immediately
r = read file
R = read line from file
s = substitute
t = test for substitution
T = test for no substitution
y = simple character replace
w = write to file
W = write line to file
z = clear line (zap)
= = print line number


syntax of delete command
delete lines at specified address (line number or line matching pattern)
[address] d
delete lines from address1 to address2
[address1],[address2] d

substitution command

replace file contents & dump to console
sed 's/[pattern]/[replacement]/g' [file]
substituting & write back to another file
sed 's/[regex]/[replacement]/g' [inputFileName] > [outputFileName]
substituting in original file
sed -i 's/[regex]/[replacement]/' [file]
wildcards can be used to modify multiple files in place
sed -i 's/[regex]/[replacement]/' *.[extension]
easier to read/write patterns => ?, {, }, (, ) and | no longer require '\' escaping
sed -r 's/[regex]/[replacement]/' [file]

substitution with address (ranges)

substitute on line(s) matching [n]
sed '[n]s/[search]/[replace]/g'
apply replacement on lines [n] to [m]
sed '[n],[m]s/[search]/[replace]/g'

substitution flags

i = ignore case
g = replace all occurrences (instead of first only)
n = replace the specified occurrence by index (among all matches)
p = print on match
w = write matches to file (must be last flag)
e = execute pat space on command line (gnu extension)
m = multi line matching mode
write to file alternatives
sed -n '/[pattern]/[replacement]/w [file out]' [file in]
sed -n '/[pattern]/[replacement]/' [file in] > [file out]

chaining sed commands

chaining sed commands with -e or ';'
seq 5 | sed -e 2d -e 4d
seq 5 | sed '2d; 4d'
piping sed
[command] | sed [sed expression]

address ranges

general form
sed [range from](,[range to])?[command]
perform command at line [n]
sed [n][command]
perform command at all lines EXCEPT [n]
sed [n]![command]
perform command from line [n] to [m] inclusive
sed [n],[m][command]
perform command everywhere EXCEPT lines [n] to [m]
sed [n],[m]![command]
repeat command every [r] lines starting at [n]
sed [n]~[r][command]
apply multiple statements to address range by grouping with '{' and '}'
sed '[n]{[statement];[statement]}'
sed '[n],[m]{[statement];[statement]}'



replace first a with b
sed 's:a:b:'
replace all a's with b's
sed 's:a:b:g'
replace first character (any) with an x
sed 's:.:x:'
replace first literal dot '.' with x
sed 's:\.:x:'
replace first zero or more a's with a single b
sed 's:a*:b:'
replace first digit with an x
sed 's:[0-9]:x:'
replace the numbers 2,3,6 & 7 with an x
sed    's/[2-36-7]/x/'
sed    's/[2-3]\|[6-7]/x/'
sed -r 's/[2-3]|[6-7]/x/'
replace numbers of at least 3 digits with xxx
sed    's/[0-9]\{3,\}/xxx/g'
sed -r 's/[0-9]{3,}/xxx/g'
replace tabs with 4 spaces
sed 's/\t/    /'
end path with sindle trailing slash
sed -e 's:/*$:/:'
increment numbers
sed    's/[0-9]\+/expr 1 + &/e'
sed -r 's/[0-9]+/expr 1 + &/e'


keep only the first character
sed    's:\(.\).\+:\1:'
sed -r 's:(.).+:\1:'
swap the first 2 characters
sed    's:\(.\)\(.\):\2\1:'
sed -r 's:(.)(.):\2\1:'
capture entire line with ampersand (&)
seq 5 | sed 's/.*/& sucks/'


delete line number 2
sed 2d
delete last line
sed '$ d'
delete line 2 to 6
sed 2,6d
delete line 2, 5, 8, 11, etc. (gnu extension)
sed '2~3d'
keep line 2, delete the rest
sed '2!d'
deleting blank lines
sed '/^ *$/d' input.txt
delete any line containing a 0 or 5 and the 2 lines after it (gnu extension)
sed '/[05]/,+2d'
delete first occurrence of foo
sed "1,/$foo/{/$foo/ d}"


insert [text] at line #1 (and append the rest of the file)
sed "1i[text]"

address ranges

extract chunk starting at pattern
sed '/[pattern]/,$ d' [file]
extract chunk until pattern:
sed '/[pattern]/q' [file]
extract chunk from pattern until pattern
sed -n '/[pattern]/,/[pattern]/p' [file]
extract line numbers 120 to 140
sed -n 120,140p [file]
use +[number] for m to specify lines relative to [n], example: extracting line 3 to 5
sed -n '3,+2p' [file]
delete every other line
sed 2~2d [file]
a dollar refers to the last line, example: counting new lines in file:
sed -n "$=" [file]
perform multiple substitutions on line 1 to 3
sed '1,3{s/apple/pear/;s/pineapple/banana/;/grapefruit/d}'

upper / lower case

upper case the first letter on every line
sed 's/[a-z]/\u&/'
upper case the first word on every line
sed 's/\w\+/\U&/'
repeat line upper case, input case and finally lower case
sed 's/.*/\U& \E& \L&/'