Replacing text for all files in a directory

Kemp Randy-W18971 Randy.L.Kemp at
Wed Aug 8 14:45:44 CEST 2001

Thanks.  I will look into the shell script, Python, and Perl suggestions

-----Original Message-----
From: Manoj Plakal [mailto:plakal-nospam at]
Sent: Tuesday, August 07, 2001 11:25 PM
To: python-list at
Subject: Re: Replacing text for all files in a directory

Grant Edwards wrote:
> In article <mailman.997215734.1690.python-list at>, 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.


More information about the Python-list mailing list