Table of contents
Introduction
sed
is a powerful stream editor that allows you to perform text transformations
on input streams or files. It is commonly used in Bash scripts for tasks like
search and replace, text substitution, and pattern matching.
Various ways of using sed
# multi command echo "hello world" | sed -e 's/hello/Hello/g' -e 's/world/Stream EDitor/g'
# read patterns from sed file echo 's/hello/Hello/g' >> hello.sed echo 's/world/Stream EDitor/g' >> hello.sed echo "hello world" | sed -f hello.sed Hello World
# create backup befor any change echo "hello world" > hello.txt sed 's/world/sed/g' -i.bk hello.txt # inline change without backup sed 's/world/sed/g' -i hello.txt
Repeated patterns
print lines
sed -n '1,10p' input_file
This command prints lines 1 to 10 from the input file. The -n
flag suppresses
automatic printing, and the p
command explicitly prints the specified range of
lines.
Insert and Append
sed '2i\This is a new line' input_file
This command inserts "This is a new line" before line 2 in the input file. The
i
command is used for inserting text at a specified line number.
Similarly, you can use the a
command to append text after a specific line.
delete lines
# delete the last line of a file sed '$d' # delete the first 5 lines of a file sed '1,5d' # delete from the 10th line to the last line of a file sed '10,$d' # delete ALL lines matched pattern from a file sed '/pattern/d' # delete ALL blank lines from a file sed '/^$/d' # delete all leading blank lines at top of file sed '/./,$!d' # delete all trailing blank lines at end of file sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' # works on all seds
comment/uncommend
sed '/<pattern>/s/^/#/g' # (to comment out) sed '/<pattern>/s/^#//g' # (to uncomment) sed '3,5s/^#//' # (to uncomment lines between 3 and 5)
change variable value
sed 's/var=.*/var=new_value/'
convert CRLF to LF
# DOS/Windows to Unix sed 's/\r$//' # Unix to DOS/Windows sed 's/$/\r/'
substitute first or last in file
# substitute first sed '1 s/foo/bar/g' # substitute last sed '$s/foo/bar/g'
substitute if pattern
# substitute if pattern matched sed '/pattern/ s/foo/bar/g' # substitute if pattern not matched sed '/pattern/! s/foo/bar/g'
Resources
catonmat sed.stream.editor.cheat.sheet
Tags of this Post: