Need a shell script to traverse thru dirs and change things in files

Need a shell script to traverse thru dirs and change things in files

Post by Sherman Hsi » Mon, 10 Jul 1995 04:00:00



Hi, I need a script to traverse though a directory and make a particular
phrase disappear in the documents traversed. For example I would like
to remove the word "berkeley" in all the files under the extension .doc.
Please email me or post here if you know of a little script. Thank You.

        -Sherman

 
 
 

Need a shell script to traverse thru dirs and change things in files

Post by William D. Madd » Tue, 11 Jul 1995 04:00:00


: Hi, I need a script to traverse though a directory and make a particular
: phrase disappear in the documents traversed. For example I would like
: to remove the word "berkeley" in all the files under the extension .doc.
: Please email me or post here if you know of a little script. Thank You.

:       -Sherman

Sounds like the 'find' command is what you want, with an -exec option of
grep.

Sincerely - Bill Maddox

--
Perfect Union Lodge #10, SA Tx        Ventura Council #15 of Cryptic Masons
Oxnard Lodge #341, Oxnard, CA         Ventura Commandery #18 of KT
Oxnard Chapter of Royal Arch Masons   Channel Islands Council #114 of AMD

 
 
 

Need a shell script to traverse thru dirs and change things in files

Post by Steffen Ullri » Wed, 12 Jul 1995 04:00:00




>Hi, I need a script to traverse though a directory and make a particular
>phrase disappear in the documents traversed. For example I would like
>to remove the word "berkeley" in all the files under the extension .doc.
>Please email me or post here if you know of a little script. Thank You.

>    -Sherman

for i in `find . -name "*.doc" -type f -print `; do
        echo "1,$ s/berkeley//^Jw^Jq" | ed $i
        done

--
                                        Steffen Ullrich

                    http://www.xensei.com/users/ccrlphr/

 
 
 

Need a shell script to traverse thru dirs and change things in files

Post by Kevin Woo » Wed, 12 Jul 1995 04:00:00


: Hi, I need a script to traverse though a directory and make a particular
: phrase disappear in the documents traversed. For example I would like
: to remove the word "berkeley" in all the files under the extension .doc.
: Please email me or post here if you know of a little script. Thank You.

:       -Sherman

files=`find . -type f -print | xargs grep -l "$1"`

for V in $files
do
        v=`echo $V | sed 's!.*/!!'`

        sed "s/$1//g" < $V > /tmp/${v}.$$ && rm -f $V
        mv /tmp/${v}.$$ $V
done

this is ugly, hard to read, and full of errors if the wrong input is
entered as the first argument. it does try to make sure the sed and
redirect worked before it removes the old file...

anyway, it does what you want to a certain extent. you can modify it
to check for $1 and also take additional args for file matches like
.doc, .txt, etc.

kevin