11 November 2007

Bulk search and replace using sed

This little snippet replaces occurrences of oldString with newString in all files named *.txt in the current directory and below, keeping a backup of the original files.

for i in $(find . -name "*.txt"); do sed 's/oldString/newString/g' $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done

1 comment:

  1. Example of replacing the svn root in a bunch of svn entry files.

    $ for i in `find . -name "entries"`; do sed 's/svn\.parwy\.net\/repos\/projectx/projectx-server\.host\.com\/svn/g' $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done

    $ for i in `find . -name "entries"`; do rm -f $i-backup; done

    ReplyDelete