Skip to content

Instantly share code, notes, and snippets.

@prestontimmons
Created March 29, 2010 17:42
Show Gist options
  • Save prestontimmons/348152 to your computer and use it in GitHub Desktop.
Save prestontimmons/348152 to your computer and use it in GitHub Desktop.
Search and Replace in Linux

Search and Replace Utilities

sed

The sed command makes it easy to modify text.

$ sed -i "s/django.conf.urls.defaults/django.conf.urls/g" urls.py

You can edit a files contents inplace using the (-i) flag. Multiple files can be passed to sed using the xargs command.

$ find -name "*.html" | xargs sed -i "s/<text to change>/<new text>/g" 

Perl

Perl is well-suited to search and replace. It can also handle multi-line replacements, which are difficult with sed.

perl -pe "BEGIN{undef $/;} s/<start>.*<stop>/<new text>/smg" <filename>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment