Home   Back

Sed Cheatsheet

Basic Syntax

sed [options] 'command' files



[address-range]/pprintPrint [specified address range]
[address-range]/ddeleteDelete [specified address range]
s/pattern1/pattern2/substituteSubstitute pattern2 for first instance of pattern1 in a line
[address-range]/s/pattern1/pattern2/substituteSubstitute pattern2 for first instance of pattern1 in a line, over address-range
[address-range]/y/pattern1/pattern2/transformreplace any character in pattern1 with the corresponding character in pattern2, over address-range (equivalent of tr)
[address] i pattern FilenameinsertInsert pattern at address indicated in file Filename. Usually used with -i in-place option.
gglobalOperate on every pattern match within each matched line of input


8dDelete 8th line of input.
/^$/dDelete all blank lines.
1,/^$/dDelete from beginning of input up to, and including first blank line.
/Jones/pPrint only lines containing "Jones" (with -n option).
s/Windows/Linux/Substitute "Linux" for first instance of "Windows" found in each input line.
s/BSOD/stability/gSubstitute "stability" for every instance of "BSOD" found in each input line.
s/ *$//Delete all spaces at the end of every line.
s/00*/0/gCompress all consecutive sequences of zeroes into a single zero.
echo "Working on it." | sed -e '1i How far are you along?'Prints "How far are you along?" as first line, "Working on it" as second.
5i 'Linux is great.' file.txtInserts 'Linux is great.' at line 5 of the file file.txt.
/GUI/dDelete all lines containing "GUI".
s/GUI//gDelete all instances of "GUI", leaving the remainder of each line intact.