Title here
Summary here
Command | Description | Example |
---|---|---|
p | Print the current line | sed -n '2p' file.txt |
d | Delete the current line | sed '3d' file.txt |
q | Quit after the first match | sed '/pattern/q' file.txt |
n | Read the next line into pattern space | sed 'n;s/old/new/' file.txt |
N | Append the next line to pattern space | sed 'N;s/\n/ /' file.txt |
Command | Description | Example |
---|---|---|
1,3d | Delete lines 1 to 3 | sed '1,3d' file.txt |
2p | Print the second line | sed -n '2p' file.txt |
5,$d | Delete from line 5 to the end | sed '5,$d' file.txt |
/pattern/d | Delete lines containing pattern | sed '/error/d' file.txt |
/pattern/!d | Delete lines NOT matching pattern | sed '/keep/!d' file.txt |
/#/d | Delete lines containing a hash symbol | sed '/#/d' file.txt |
Command | Description | Example |
---|---|---|
s/old/new/ | Substitute first occurrence | sed 's/old/new/' file.txt |
s/old/new/g | Substitute all occurrences (global) | sed 's/old/new/g' file.txt |
s/old/new/2 | Substitute second occurrence only | sed 's/old/new/2' file.txt |
s/old/new/i | Case-insensitive substitution | sed 's/old/new/i' file.txt |
s/old/new/p | Print line after substitution | sed 's/old/new/p' file.txt |
1,3s/old/new/g | Substitute globally on lines 1 to 3 | sed '1,3s/old/new/g' file.txt |
Command | Description | Example |
---|---|---|
s/^start/replace/ | Match at beginning of line | sed 's/^start/replace/' file.txt |
s/end$/replace/ | Match at end of line | sed 's/end$/replace/' file.txt |
s/\bword\b/replace/ | Match whole word only | sed 's/\bword\b/replace/' file.txt |
s/\<word\>/replace/ | Alternative whole word match | sed 's/\<word\>/replace/' file.txt |
Command | Description | Example |
---|---|---|
s/[0-9]/X/g | Replace all digits with X | sed 's/[0-9]/X/g' file.txt |
s/[^0-9]//g | Remove non-numeric characters | sed 's/[^0-9]//g' file.txt |
s/[aeiou]//g | Delete all vowels | sed 's/[aeiou]//g' file.txt |
s/[^aeiou]//g | Delete all consonants | sed 's/[^aeiou]//g' file.txt |
s/\<[0-9]\+\>//g | Remove whole numbers | sed 's/\<[0-9]\+\>//g' file.txt |
Command | Description | Example |
---|---|---|
s/\(.*\),\(.*\)/\2 \1/ | Swap comma-separated fields | sed 's/\(.*\),\(.*\)/\2 \1/' file.txt |
s/\(pattern\)/\1/g | Backreference matched pattern | sed 's/\([0-9]\)/[\1]/g' file.txt |
s/\([0-9]\)/\1\1/g | Duplicate each digit | sed 's/\([0-9]\)/\1\1/g' file.txt |
s/\(.\)/\1 /g | Add space after each character | sed 's/\(.\)/\1 /g' file.txt |
Command | Description | Example |
---|---|---|
`s/^[[:space:]]*//' | Trim leading whitespace | sed 's/^[[:space:]]*//' file.txt |
`s/[[:space:]]*$//' | Trim trailing whitespace | sed 's/[[:space:]]*$//' file.txt |
s/^[[:space:]]*//;s/[[:space:]]*$// | Trim both leading and trailing | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' file.txt |
s/\n/ /g | Replace newlines with spaces | sed 's/\n/ /g' file.txt |
s/ */ /g | Collapse multiple spaces to one | sed 's/ */ /g' file.txt |
Command | Description | Example |
---|---|---|
s/\(.*\)/\U\1/ | Uppercase entire line | sed 's/\(.*\)/\U\1/' file.txt |
s/\(.*\)/\L\1/ | Lowercase entire line | sed 's/\(.*\)/\L\1/' file.txt |
s/^\(.\)/\U\1/ | Uppercase first character | sed 's/^\(.\)/\U\1/' file.txt |
s/^\(.\)/\L\1/ | Lowercase first character | sed 's/^\(.\)/\L\1/' file.txt |
Command | Description | Example |
---|---|---|
s/^/# / | Comment out lines with # | sed 's/^/# /' file.txt |
s/^[^#].*$/# &/ | Comment out non-comment lines | sed 's/^[^#].*$/# &/' file.txt |
s/^# // | Uncomment lines | sed 's/^# //' file.txt |
Command | Description | Example |
---|---|---|
a\text | Append text after line | sed '2a\New line' file.txt |
i\text | Insert text before line | sed '2i\New line' file.txt |
c\text | Change/replace entire line | sed '2c\Replacement line' file.txt |
r filename | Read file and insert contents | sed '2r other.txt' file.txt |
w filename | Write pattern space to file | sed '/pattern/w output.txt' file.txt |
Command | Description | Example |
---|---|---|
h | Copy pattern space to hold space | sed '1h;2g' file.txt |
H | Append pattern space to hold space | sed '1h;2H;3g' file.txt |
g | Copy hold space to pattern space | sed '1h;3g' file.txt |
G | Append hold space to pattern space | sed '1h;2G' file.txt |
x | Exchange pattern and hold space | sed '1h;2x' file.txt |
Command | Description | Example |
---|---|---|
y/abc/123/ | Transliterate characters a→1, b→2, c→3 | sed 'y/abc/123/' file.txt |
l | Display non-printing characters | sed -n 'l' file.txt |
= | Print line number | sed -n '3=' file.txt |
!command | Run shell command | sed '1!d' file.txt |
; | Command separator | sed 's/old/new/;s/foo/bar/' file.txt |
Command | Description | Example |
---|---|---|
sed -n '1,5p' | Print lines 1 to 5 | sed -n '1,5p' file.txt |
sed '1d;$d' | Remove first and last line | sed '1d;$d' file.txt |
sed '/^$/d' | Remove empty lines | sed '/^$/d' file.txt |
sed 's/.*/"&"/' | Quote entire line | sed 's/.*/"&"/' file.txt |
sed -n '/start/,/end/p' | Print between patterns | sed -n '/start/,/end/p' file.txt |