Skip to content

Replace multiple strings using sed

How to replace multiple strings using sed?

Answer:

If you have multiple strings to replace, you can either using the "-e" option in sed, e.g.

sed -e 's/a/A/' -e 's/b/B/' < old.txt > new.txt

or, you can use a sed script file to store all the replace conditions

e.g. vi replace.sed

s/a/A/g
s/b/B/g
s/c/C/g

and execute like

sed -f replace.sed < old.txt > new.txt

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  1. sed: replace in place
  2. Replace infile using sed and make a backup automatically
  3. How to replace a string in a file using Perl
  4. How to split a large file into multiple smaller files in Linux
  5. Search and replace string in file using Perl

One Comment

  1. Or you can include all in a single, newline-separated string:

    sed 's/a/A/g
    s/b/B/g
    s/c/C/g' new.txt

    Saturday, December 26, 2009 at 6:15 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*