Skip to content

sed command

Edit text in a stream: replace, delete or insert lines.

On this page

Syntax

sed [options] 'script' [file…]

Provided by the sed package. Run man sed for the full manual.

Common options

OptionWhat it does
-iEdit files in place
-i.bakEdit in place and keep a backup
-nPrint only lines you ask for with p
-EExtended regular expressions
-e SCRIPTAdd several commands
s/old/new/gReplace every match on each line
/pattern/dDelete matching lines
NpPrint line N (with -n)

Examples

  1. Replace text and print the result

    sed 's/http:/https:/g' urls.txt
  2. Replace in the file itself

    sed -i 's/old/new/g' config.txt
  3. Replace and keep a backup

    sed -i.bak 's/8080/9090/' app.conf
  4. Delete lines containing a word

    sed -i '/DEBUG/d' app.log
  5. Delete blank lines

    sed -i '/^$/d' file.txt
  6. Delete the first line

    sed -i '1d' data.csv
  7. Print lines 10 to 20

    sed -n '10,20p' file.txt
  8. Insert a line before line 3

    sed -i '3i new line' file.txt
  9. Uncomment a line

    sed -i 's/^#\(PermitRootLogin\)/\1/' /etc/ssh/sshd_config
  10. Use another delimiter for paths

    sed 's|/var/www|/srv/www|g' site.conf

Tips

  • On macOS, sed -i needs an argument: sed -i '' 's/a/b/' file.

sed guides

← All Linux commands · Suggest a fix

Back to top