Sed - Cheatsheet

By xngo on February 28, 2019

Helpful Sed one-liners commands.

REM Delete empty line
sed -e "/^$/d" 1x.txt > 2x.txt

REM Delete all newline.( Concatenate each line of 1x.txt into 1 line of string )
sed ":a;N;$!ba;s/\n//g" 1x.txt > 2x.txt
REM http://sed.sourceforge.net/sedfaq5.html#s5.10

REM Print line without 'code'
sed -n "/code/!p" 1x > 2x.txt
REM In bash shell, use single quote.

REM Delete line 2 up to line 10 inclusively.
sed -e "2,10d" 1x.txt > 2x.txt

REM Skip the 3rd line(Replace 'a' with 'b' for all lines except the 3rd line).
sed -e "3n; s/a/b/g" 1x.txt > 2x.txt

REM Insert LINE at line 3.
REM If no number before i is defined, then sed will insert LINE before every line in 1x.txt
sed -i "3i\LINE" 1x.txt

REM Append WORD at the end of file.
sed -i "$a\WORD" 1x.txt

REM Use semi-colon(;) as OR operator.
REM Print line containing expression1 or expression2 or etc
sed -n "/exp1/p; /exp2/p" 1x.txt

REM Number each line of a file
sed = filename.txt | sed "N;s/\n/\t:/" > 1x.txt

REM Replace empty lines with previous non-empty line.
sed -e "/[^ ]/{h}" -e g input.txt
REM http://www.computing.net/unix/wwwboard/forum/7768.html

REM Copy pattern found multiple times using ampersand(&)
ECHO 123 abc | sed "s/123/& | & | &/"

REM Copy pattern found multiple times using parentheses.
ECHO 123 abc | sed -e "s/\(123\)\(.*\)/ \1 \1 \2 \1 \2 /"

REM Process every 4th line.
sed -e "0~4s/$/ReplaceString/g" input.txt > output.txt

Classes

     [[:alnum:]]  - [A-Za-z0-9]     Alphanumeric characters
     [[:alpha:]]  - [A-Za-z]        Alphabetic characters
     [[:blank:]]  - [ \x09]         Space or tab characters only
     [[:cntrl:]]  - [\x00-\x19\x7F] Control characters
     [[:digit:]]  - [0-9]           Numeric characters
     [[:graph:]]  - [!-~]           Printable and visible characters
     [[:lower:]]  - [a-z]           Lower-case alphabetic characters
     [[:print:]]  - [ -~]           Printable (non-Control) characters
     [[:punct:]]  - [!-/:-@[-`{-~]  Punctuation characters
     [[:space:]]  - [ \t\v\f]       All whitespace chars
     [[:upper:]]  - [A-Z]           Upper-case alphabetic characters
     [[:xdigit:]] - [0-9a-fA-F]     Hexadecimal digit characters
http://www.student.northpark.edu/pemente/sed/sedfaq3.html
REM Print only if there are 3 consecutive hexadecimal.
echo a1f | sed -n "/[[:xdigit:]]\{3\}/p"

Regular Expression

Any character(.)
Dot(.) represents any character.
Negation(^)
# (In bash) Remove everything that is not >
echo 'a >bc>d  e' | sed -e 's/[^>]//g'
# Remove tags(<..>)
echo '<h1>Remove</h1> <em>html/xml</em> tags.' | sed -e 's/<[^>]*>//g'
Express OR expression inside square brackets([])
# Replace a or f with X.
echo 'abcdefghi' | sed -e 's/[af]/X/g'
Range(-)
# Replace characters ranging from c to f inclusively with X.
echo 'abcdefghi' | sed -e 's/[c-f]/X/g'
Defined repeating occurrences({})
# Replace ccc with X. Don't forget to put backslash before curly brackets.
echo 'abccdecccfghi' | sed -e 's/c\{3\}/X/g'
Infinite repeating occurrences(*)
# Replace any characters(.) between a and ; inclusively with X.
echo 'ab1$2c3+d;efghi' | sed -e 's/a.*;/X/g'

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.