From kevin@ti.com Tue May 27 20:12:43 1997 Path: fu-berlin.de!news.mathworks.com!cam-news-hub1.bbnplanet.com!cpk-news-hub1.bbnplanet.com!cpk-news-feed3.bbnplanet.com!news.bbnplanet.com!news.ti.com!not-for-mail From: kevin@ti.com (Kevin Earls) Newsgroups: comp.editors Subject: Re: Help: search /replace text in multiple files using SED Date: 22 May 1997 19:14:34 GMT Organization: Texas Instruments Lines: 31 Distribution: inet Message-ID: <5m25uq$n52$1@tilde.csc.ti.com> References: <3384ACC2.47E4@charweb.org> Reply-To: kevin@ti.com NNTP-Posting-Host: ews-sherman35.sh-gpl.ti.com In article <3384ACC2.47E4@charweb.org>, Tony Dick writes: >Anybody know how to search/replace text in multiple files using SED? I >can do it with one using sed 's/color/colour/g' filename, but how to do >all files within a given directory? > >Tony Dick >Charlotte, NC Dos: > for %file in (*.*) do sed 's/color/colour/g' %file csh and tcsh: % foreach file (*) foreach? sed 's/color/colour/g' $file foreach? end sh and family: $ for file in *; do > sed 's/color/colour/g' $file > done Kevin -- _/_/_/_/_/ _/_/_/ Kevin Earls _/ _/ kevin@ti.com _/ _/ Product Engineering _/ _/ Texas Instruments _/ _/_/_/ Sherman, TX From pwd@pwyz.rhein.de Tue May 27 20:12:49 1997 Path: fu-berlin.de!news.apfel.de!cpk-news-hub1.bbnplanet.com!news.bbnplanet.com!newsfeed.internetmci.com!demos!news.uni-stuttgart.de!news.ruhr-uni-bochum.de!news.rhrz.uni-bonn.de!rhein!phoenix.rhein.de!pwyz.rhein.de!pwd From: pwd@pwyz.rhein.de (Peter Wyzlic) Newsgroups: comp.editors Subject: Re: Help: search /replace text in multiple files using SED Date: 22 May 1997 23:55:44 +0200 Organization: Not organized Lines: 53 Sender: pwyzlic@old.pwyz.rhein.de Distribution: inet Message-ID: References: <3384ACC2.47E4@charweb.org> Reply-To: pwyzlic@pwyz.rhein.de NNTP-Posting-Host: old.pwyz.rhein.de X-Newsreader: slrn (0.9.3.2 UNIX) On Thu, 22 May 1997 13:29:54 -0700, Tony Dick wrote: >Anybody know how to search/replace text in multiple files using SED? I >can do it with one using sed 's/color/colour/g' filename, but how to do >all files within a given directory? Assuming you are using a standard Unix shell you can use a for-loop, like this: for all in /home/peter/*.txt do sed 's/abc/def/g' $all > /tmp/$all done You will find all the changed *.txt files in /tmp with the original names. If you are sure your sed script is clean you may write a shell script which does the following: 1. invoking sed in a for-loop 2. writing the changed files into a temporary directory 3. overwriting the original files by the changed ones, e.g. via cp /tmp/$all $all or mv /tmp/$all $all I append a shell script "runsed" which is to be found in D. Dougherty's "sed and awk". It assumes you have written your sed commands into a file called "sedscr". You need sh, cmp and a directory /tmp: -------------------------------------------- #!/bin/sh # From Dale Dougherty: sed & awk for x do echo -e "editing $x: \c" if test "$x" = sedscr; then echo "not editing sedscript!" elif test -s $x; then sed -f sedscr $x > /tmp/$x if test -s /tmp/$x then cmp -s $x /tmp/$x && echo -e "file not changed: \c"; cp /tmp/$x $x; echo "done" else echo "sed produced an empty file." fi else echo "original file is empty" fi done echo "all done" -------------------------------------------- Hope it helps \bye -- Peter Wyzlic ................................. Bonn ... fugit inreparabile tempus ... .................................