SED CheatSheet
SED CheatSheet
github.com/Randy8080/reference/blob/main/sed.md
Sed Usage
Syntax
With pipeline
{.show-header}
Multiple commands
Sed script
Examples
1/5
See: Sed examples
Commands {.col-span-2}
p sed -n -e '1,4 p' -e '6,7 p' input.txt Print lines 1-4 and 6-7
{.show-header}
Space commands
Command Description
n Print pattern space, empty pattern space, and read next line
Flags
Flag Description
g Global substitution
Loops commands
Command Description
2/5
Command Description
Misc Flags
Flag Description
`/ ^ @ ! #`
Replace "world" with "universe" but only if the line begins with "hello"
Search for a string but only output lines that do not match
Appending lines
Append line after line 2
3/5
$ sed '$a THE END!' file.txt
Numbering {.col-span-2}
Number line of a file (simple left alignment)
Number line of file, but only print numbers if line is not blank
$ sed -n '$='
Prepending lines
Insert text before line 5
Deleting lines
Delete line 5-7 in file
File spacing
Double space
$ sed G
$ sed '/^$/d;G'
$ sed 'G;G'
Undo double-spacing
4/5
$ sed 'n;d'
$ sed '/regex/{x;p;x;}'
$ sed '/regex/G'
$ sed '/regex/{x;p;x;G;}'
5/5