Replacing text for all files in a directory

Manoj Plakal plakal-nospam at nospam-cs.wisc.edu
Wed Aug 8 00:25:09 EDT 2001


Grant Edwards wrote:
> 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

        Perl has some useful command-line options
            for doing in-place edits:

              perl -p -i -e "s/foo/bar/g;"  dir/*

           will replace "foo" with "bar" in all files inside directory
           "dir". But beware the power of the Dark Side,
           this stuff can degenerate rapidly if you are tempted
           to do more complicated stuff with it.

           Manoj





More information about the Python-list mailing list