sed Commands Cheat Sheet

• Updated January 10, 2019


sed is an extremely powerful stream editor. The web is full of useful sed one liners.

This post will be an ever growing list of sed one liners I have found useful.

Find and Replace

Find and remove all instances of TEXT:

sed 's/TEXT//g'

Find and remove all spaces:

sed -e 's/ //g'

Delete all blank lines from a file

sed '/^$/d' <FILE>

Delete leading whitespace from front of each line

sed 's/^[ \t]*//' <FILE>

Delete trailing whitespace from end of each line

sed 's/[ \t]*$//' <FILE>

Delete leading and trailing whitespace from each line

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

Insert a line of text before a line

sed -i '/PATTERN/i REPLACEWITH' <FILE>

Change configuration file values

Change BRIDGE_HOTPLUG=yes to BRIDGE_HOTPLUG=no no matter what it is already set to:

sed '/BRIDGE_HOTPLUG=/ s/=.*/=no/' /etc/default/bridge-utils

Change PermitRootLogin no to PermitRootLogin yes no matter what it is already set to:

sed '/PermitRootLogin / s/ .*/ yes/' /etc/ssh/sshd_config

Wrap Value in Double Quotes

Change date=2015 to date="2015”:

echo 'date=2015' | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/'

Remove word wrap

sed -e :a -e '$!N;s/\n //;ta' -e 'P;D' <FILE>
sed -n 's/.*href="\([^"]*\).*/\1/p' <FILE.html>

Lower case all href=”*” tags

sed -e 's/\(href="\([^"]*\)"\)/\L\1/' <FILE>

Lower case all img src=”*” tags

sed -e 's/\(img src="\([^"]*\)"\)/\L\1/' <FILE>

Lower case all href=”” tags but exclude href="http” tags

sed -e '/\(href="http"*\)/!s/\(href="\([^"]*\)"\)/\L\1/' <FILE>

Lower case all img src=”” tags but exclude img src="http” tags

sed -e '/\(img src="http"*\)/!s/\(img src="\([^"]*\)"\)/\L\1/' <FILE>

Remove ‘%20’ only from lines matching href=”*”

sed -e '/\(href="\([^"]*\)"\)/s/%20//g' <FILE>

References

If you found this post useful, you can help support this site and get something for yourself by signing up for any of the services listed below through the provided links.

The following links are part of affiliate programs, and I will receive a referral payment from any of the services you sign-up for.

  • Get Faster Shipping: Perhaps you're about to order some stuff on Amazon but want to get more value out of the money you would normally pay for shipping. Sign-up for a free, 30-day trial of Amazon Prime to get free two-day shipping, access to thousands of movies and TV shows, and more.
  • Start Investing Now: Are you looking to start investing some of your disposable saved income? Get started by opening an account with Webull, where upon sign-up and opening a brokerage account with an initial deposit of at least $100 you will receive 1 stock valued (subject to change based on normal market movement) between $8 and $1600.

Thanks for reading and take care.