sed by examples Linux 31.05.2014

sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language. (wiki)

sed gets input stream (usually file) line by line, modify each line accordingly to rules in sed's script file and print the result to output stream.

Following is example of command

sed [options] '{script}' [file(s)]

If there is no file as input then sed reads input stream or pipeline. If you want the result in a text file, you can either redirect it via the regular method:

sed [options] '{script}' [inputfile] > [output]

Or use the option -i that will directly edit the input file

sed -i [options] '{script}' [inputfile]

Search

Command Description
sed -n "3p" file.txt print 3rd line
sed -n '1,3p' file.txt print strings from 1st to 3rd
sed -n "/[media]/,/[public]/p" smb.conf print strings from 1st to 3rd which contains specified string
sed -n '/[media]/,$p' smb.conf print strings from first mention of media till end of file
sed 5,15d file.txt show all of file.txt except for lines from 5 to 15
sed '/pattern/q' file.txt print all lines till pattern
sed -n '/pattern/p' file.txt print do matched strings (emulates grep)
sed -n '/pattern/!p' file.txt print do NOT matched strings (emulates grep -v)
sed -n 's/unix/linux/p' file.txt printing only the replaced lines
sed -n '/X/!p' file.txt print lines which does not contain 'X'
sed -n '2~5p' file.txt print every 5th line starting with the second
sed '/AAA.*BBB.*CCC/!d' file.txt grep for AAA and BBB and CCC (in that order)
sed '/AAA/!d; /BBB/!d; /CCC/!d' file.txt grep for AAA and BBB and CCC (in any order)

Modify

Command Description
sed 's/foo/boo/' file.txt replace first entrance foo with boo
sed 's|http://|www|' file.txt replace first entrance foo with boo (another divider)
sed 's/foo/boo/g' file.txt replace all entrances foo with boo
sed 's/foo/boo/3' file.txt replace first three entrances foo with boo
sed 's/foo/[&]/' file.txt wrap foo with [ ]
sed 's/pattern1|pattern2/foo/g' file.txt replace pattern1 or pattern2 with foo
sed 's/\(foo\)\(boo\)/\2\1/' file.txt replace fooboo with boofoo
sed 's/^../XX/' file.txt replace the first two characters of a string or a line with "XX"
sed -e 's/unix/linux/' -e 's/os/system/' file.txt -e option provieds to run multiple sed commands in a single sed command
sed '3 s/foo/boo/' file.txt replace string on a specific line number
sed '1,3 s/foo/boo/' file.txt replace string on a range of lines
sed '/pattern/ s/foo/boo/' file.txt replace on a lines which matches a pattern
sed '1i HEAD1, HEAD2' file.txt insert a header line
sed -i '1a -------' file.txt add a line '-------' at the 1st line
sed '5!s/foo/boo/' file.txt replace foo with boo in file.txt except in the 5th line
sed '/pattern/!s/foo/boo/' file.txt unless pattern is found replace foo with boo
sed '/pattern/s/foo/boo/g' file.txt only if line contains pattern, substitute foo with boo
sed '1d;$d' file.txt delete the first line AND the last line of a file
sed '/^$/d' file.txt delete all blank lines in the file
sed '/foo/,$d' file.txt delete the lines starting from the pattern 'foo' till the last line
sed '1,4{/foo/d;}' file.txt delete the lines containing the pattern 'foo' only if it is present in the lines from 1 to 4
sed '1,20 s/foo/boo/g' file.txt replace foo with boo only on lines between 1 and 20
sed '1,20 !s/foo/boo/g' file.txt the above reversed (match all except lines 1-20)
sed '/pattern/G' file.txt insert blank line below every line that match pattern
sed 'n;n;n;n;G;' file.txt add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
sed 's/^/ /' file.txt insert 5 blank spaces at beginning of each line (make page offset)
sed '/foo/ a "boo"' file.txt add a line after a match
sed '/foo/ i "boo"' file.txt add a line before a match
sed '/foo/ c "boo"' file.txt replace an entire line with a new line
sed ‘s/^/\t/’ file.txt insert a tab at beginning of each line
sed -e 's/#.*//' file.txt delete all the comment lines from a file
$sed 's/.$//' file.txt convert DOS newlines (CR/LF) to Unix format
sed -e '$r 2.txt' 1.txt insert file 2.txt at the end of 1.txt
sed 's/\t/ /g' file.txt replace tab with spaces
find ~/projects/ -type f -exec sed -i 's/nodejs/django/g' {} \; replace in all found files
find ~/projects/ -name "*.js" -print | xargs sed -i 's/nodejs/django/g' replace in multiple files

Delete

Command Description
sed -n "3,10d" file.txt delete all lines from line 3 till 10
sed '17,/foo/d' file.txt delete all lines from line 17 to 'foo'
sed 's/^[ ^t]*//' file.txt delete all spaces in front of every line of file.txt
sed 's/[ ^t]*$//' file.txt delete all spaces at the end of every line of file.txt
sed 's/^[ ^t]*//;s/[ ^]*$//' file.txt delete all spaces in front and at the end of every line of file.txt
sed '1~3d' file.txt delete every third line, starting with the first
sed ‘/^$/d’ in.txt > out.txt delete all the blank lines from a file
sed '0,/pattern_to_delete/{//d;}' file.txt delete only first match from a file

Useful links