Replacing text for all files in a directory

Grant Edwards grante at visi.com
Tue Aug 7 17:10:11 EDT 2001


In article <mailman.997215734.1690.python-list at python.org>, Kemp Randy-W18971 wrote:

> Great!  But how can I apply this to all files in a directory, where the
> directory contains 20 files, without listing each file individually?

      for i in *; do
 	sed -e "s/Have a good day/jump off a cliff/g" $i > tmp
 	mv tmp $i
      done
      
Better yet, put it in a file like this:

#!/bin/bash
for f
 do
 sed -e "s/Have a good day/jump off a cliff/g" $f > tmp$$
 mv tmp$$ $f
 done
 
Then you can invoke that file with a filename or a wildcard:

$ foo A B C
$ foo *
$ foo /some/path/or/other/bar[1-9][0-9]*.txt

-- 
Grant Edwards                   grante             Yow!  Excuse me, but didn't
                                  at               I tell you there's NO HOPE
                               visi.com            for the survival of OFFSET
                                                   PRINTING?



More information about the Python-list mailing list