Sed Cheat Sheet

This page contain a complete list of Sed cheat sheet

Sed Cheatsheet

CommandDescription
!commandRun a shell command on the pattern space.
/#/dDelete lines containing a hash symbol.
/pattern/!dDelete lines not matching the pattern.
1,3dDelete lines 1 to 3.
1,3s/old/new/gSubstitute globally on lines 1 to 3.
2pPrint the second line.
5!pPrint lines not matching the address 5.
5,$dDelete from line 5 to the end.
a\Append text after a line.
DDelete up to the first newline in the pattern space.
GAppend hold space to pattern space.
gGlobally substitute on all matches.
HAppend pattern space to hold space.
hCopy pattern space to hold space.
i\Insert text before a line.
lDisplay non-printing characters.
NAppend the next line to the pattern space.
pPrint the current line.
PPrint up to the first newline in the pattern space.
qQuit (exit) after the first match.
qQuit (exit) without processing further.
r file.txtRead and insert the contents of a file.
s/[[:space:]]*$//Trim trailing whitespace.
s/[^,]\+,\([^,]\+\),[^,]\+/\1/gExtract the second field in a comma-separated list.
s/[^0-9]*\([0-9]\+\)[^0-9]*/\1/gExtract numbers from a string.
s/[^0-9]//gRemove non-numeric characters.
s/[^aeiou]*\([aeiou]\)[^aeiou]*/\1/gExtract and concatenate vowels.
s/[^aeiou]//gDelete 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]/ /gUppercase and replace non-alphabetic characters with spaces.
s/\(.*\)/\U\1/;s/[^A-Z]//gUppercase 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 /gAdd a space between each character.
s/\(.\)/\1\1/gDuplicate each character.
s/\(.\)/\1\n/gInsert a newline after each character.
s/\(.\)/\L\1/gLowercase the entire line.
s/\(.\)/\L\1\E/gUppercase the first letter, lowercase the rest.
s/\(.\)/\U\1/gUppercase the entire line.
s/\(.\)/\U\1\E/gUppercase the first letter, lowercase the rest.
s/\(.\)\(.*\)/\1/gKeep only the first character of each line.
s/\(.\)\(.*\)/\2\1/gReverse the characters in each line.
s/\(.\)\(.*\)/\U\1\L\2/Capitalize the first letter of a sentence.
s/\(.\)\(.*\)\1/\2/gRemove duplicate characters.
s/\(.\)\(.*\)\1/\U\1\L\2/gTitle Case: Uppercase the first letter of each word.
s/\([0-9]\)/\1 & \1/gSurround each digit with spaces.
s/\([0-9]\)/\1\1/Duplicate each digit.
s/\([aeiou]\)//gDelete all vowels.
s/\([aeiou]\)/\1/gRemove duplicate vowels.
s/\([aeiou]\)/\1\1/gDuplicate each vowel.
s/\([aeiou]\)/\1\n/gInsert 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/gLowercase all vowels.
s/\([aeiou]\)/\L\1/gLowercase the first vowel in each word.
s/\([aeiou]\)/\U\1/g;s/\(.\)/\1 /gUppercase vowels and add a space between each character.
s/\([aeiou]\)/\U\1/gUppercase all vowels.
s/\([aeiou]\)/\U\1\E/gUppercase the first vowel in each word.
s/\([aeiou]\)\([aeiou]\)/\2\1/gSwap adjacent vowels.
s/\(pattern\)/\1/gBackreference: substitute with matched pattern.
s/\<[0-9]\+\>//gRemove whole numbers.
s/\bword\b//gDelete 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/2Substitute on the second occurrence of old.
s/old/new/Substitute old with new on the current line.
s/old/new/gSubstitute globally on the current line.
s/old/new/iCase-insensitive substitution.
s/old/new/pPrint the line after substitution.
w output.txtWrite the pattern space to a file.
xExchange pattern and hold space.
y/abc/123/Replace characters a, b, c with 1, 2, 3.