sed Cheatsheet

The most used sed commands, ready to copy and paste.

Fill in your values

Enter your own values below and every matching command updates automatically.

Replace the first match on each line

sed 's/<Search Pattern>/<Replacement>/' <File Name>

Replace all matches on each line (global)

sed 's/<Search Pattern>/<Replacement>/g' <File Name>

Replace all matches and edit the file in place

sed -i 's/<Search Pattern>/<Replacement>/g' <File Name>

Edit in place and keep a backup of the original

sed -i.bak 's/<Search Pattern>/<Replacement>/g' <File Name>

Delete lines matching a pattern

sed '/<Search Pattern>/d' <File Name>

Print only lines matching a pattern

sed -n '/<Search Pattern>/p' <File Name>

Print a specific line number

sed -n '<Line Number>p' <File Name>

Delete a specific line number

sed '<Line Number>d' <File Name>

Print a range of lines

sed -n '1,10p' <File Name>

Insert a line before a match

sed '/<Search Pattern>/i\<Replacement>' <File Name>

Append a line after a match

sed '/<Search Pattern>/a\<Replacement>' <File Name>

Replace a match only on a specific line number

sed '<Line Number>s/<Search Pattern>/<Replacement>/' <File Name>

Remove blank lines

sed '/^$/d' <File Name>

Case-insensitive replace

sed 's/<Search Pattern>/<Replacement>/gi' <File Name>

Print the total number of lines

sed -n '$=' <File Name>

Trim leading and trailing whitespace

sed 's/^[ \t]*//;s/[ \t]*$//' <File Name>

No commands match your search.