Sed Cheat Sheet
This page contain a complete list of Sed cheat sheet
Sed Cheatsheet
Command | Description |
---|---|
!command | Run a shell command on the pattern space. |
/#/d | Delete lines containing a hash symbol. |
/pattern/!d | Delete lines not matching the pattern. |
1,3d | Delete lines 1 to 3. |
1,3s/old/new/g | Substitute globally on lines 1 to 3. |
2p | Print the second line. |
5!p | Print lines not matching the address 5. |
5,$d | Delete from line 5 to the end. |
a\ | Append text after a line. |
D | Delete up to the first newline in the pattern space. |
G | Append hold space to pattern space. |
g | Globally substitute on all matches. |
H | Append pattern space to hold space. |
h | Copy pattern space to hold space. |
i\ | Insert text before a line. |
l | Display non-printing characters. |
N | Append the next line to the pattern space. |
p | Print the current line. |
P | Print up to the first newline in the pattern space. |
q | Quit (exit) after the first match. |
q | Quit (exit) without processing further. |
r file.txt | Read and insert the contents of a file. |
s/[[:space:]]*$// | Trim trailing whitespace. |
s/[^,]\+,\([^,]\+\),[^,]\+/\1/g | Extract the second field in a comma-separated list. |
s/[^0-9]*\([0-9]\+\)[^0-9]*/\1/g | Extract numbers from a string. |
s/[^0-9]//g | Remove non-numeric characters. |
s/[^aeiou]*\([aeiou]\)[^aeiou]*/\1/g | Extract and concatenate vowels. |
s/[^aeiou]//g | Delete all consonants. |
s/\(.*\),\(.*\)/\2 \1/ | Swap two comma-separated fields. |
s/\(.*\)/\L\1/;s/./\U&/ | Lowercase the entire line, then uppercase the first letter. |
s/\(.*\)/\L\1/ | Lowercase the entire line. |
s/\(.*\)/\L\1\E/ | Uppercase the first letter in each word. |
s/\(.*\)/\U\1/;s/[^A-Z]/ /g | Uppercase and replace non-alphabetic characters with spaces. |
s/\(.*\)/\U\1/;s/[^A-Z]//g | Uppercase and remove non-alphabetic characters. |
s/\(.*\)/\U\1/ | Uppercase the entire line. |
s/\(.*\)\([aeiou]\)\(.*\)/\1\3\2/ | Swap the first vowel with the last. |
s/\(.*\)\n\(.*\)/\2\1/ | Swap lines in a pattern space. |
s/\(.\)/\1 /g | Add a space between each character. |
s/\(.\)/\1\1/g | Duplicate each character. |
s/\(.\)/\1\n/g | Insert a newline after each character. |
s/\(.\)/\L\1/g | Lowercase the entire line. |
s/\(.\)/\L\1\E/g | Uppercase the first letter, lowercase the rest. |
s/\(.\)/\U\1/g | Uppercase the entire line. |
s/\(.\)/\U\1\E/g | Uppercase the first letter, lowercase the rest. |
s/\(.\)\(.*\)/\1/g | Keep only the first character of each line. |
s/\(.\)\(.*\)/\2\1/g | Reverse the characters in each line. |
s/\(.\)\(.*\)/\U\1\L\2/ | Capitalize the first letter of a sentence. |
s/\(.\)\(.*\)\1/\2/g | Remove duplicate characters. |
s/\(.\)\(.*\)\1/\U\1\L\2/g | Title Case: Uppercase the first letter of each word. |
s/\([0-9]\)/\1 & \1/g | Surround each digit with spaces. |
s/\([0-9]\)/\1\1/ | Duplicate each digit. |
s/\([aeiou]\)//g | Delete all vowels. |
s/\([aeiou]\)/\1/g | Remove duplicate vowels. |
s/\([aeiou]\)/\1\1/g | Duplicate each vowel. |
s/\([aeiou]\)/\1\n/g | Insert a newline after each vowel. |
s/\([aeiou]\)/\L\1/g;s/\(.\)/\U&/ | Uppercase the entire line, then lowercase the first letter. |
s/\([aeiou]\)/\L\1/g | Lowercase all vowels. |
s/\([aeiou]\)/\L\1/g | Lowercase the first vowel in each word. |
s/\([aeiou]\)/\U\1/g;s/\(.\)/\1 /g | Uppercase vowels and add a space between each character. |
s/\([aeiou]\)/\U\1/g | Uppercase all vowels. |
s/\([aeiou]\)/\U\1\E/g | Uppercase the first vowel in each word. |
s/\([aeiou]\)\([aeiou]\)/\2\1/g | Swap adjacent vowels. |
s/\(pattern\)/\1/g | Backreference: substitute with matched pattern. |
s/\<[0-9]\+\>//g | Remove whole numbers. |
s/\bword\b//g | Delete whole words. |
s/\bword\b/replace/ | Match whole word only. |
s/\n/ / | Replace newline with space. |
s/^/# / | Comment out lines by adding a # at the beginning. |
s/^[[:space:]]*//;s/[[:space:]]*$// | Trim leading and trailing whitespace. |
s/^[[:space:]]*// | Trim leading whitespace. |
s/^[^#].*$/# &/ | Comment out non-comment lines. |
s/^\(.\)/\L\1/ | Lowercase the first character. |
s/^\(.\)/\U\1/ | Uppercase the first character. |
s/^\(.\)\(.*\)/\1\U\2/ | Uppercase the second character. |
s/^\(.\)\(.*\)/\U\1\L\2/ | Capitalize the first letter of each word. |
s/^start/replace/ | Substitute at the beginning of a line. |
s/end$/replace/ | Substitute at the end of a line. |
s/old// | Delete instances of old on the current line. |
s/old/new/2 | Substitute on the second occurrence of old. |
s/old/new/ | Substitute old with new on the current line. |
s/old/new/g | Substitute globally on the current line. |
s/old/new/i | Case-insensitive substitution. |
s/old/new/p | Print the line after substitution. |
w output.txt | Write the pattern space to a file. |
x | Exchange pattern and hold space. |
y/abc/123/ | Replace characters a, b, c with 1, 2, 3. |