Click to See Complete Forum and Search --> : script for multi-file find and replace


mike_chach
08-20-2002, 06:53 AM
Hello all...this is my first post so please excuse my ignorance : )

I need a script or a program that will allow me to do a find and replace operation on multiple files in a folder.

Any help or pointers in the right direction would be greatly appreciated.

I just trashed windows me for mandrake linux last week and haven't had to reboot once!!! YIPPEEEE!!!!!

your friend
mike chach

binaryDigit
08-20-2002, 11:15 AM
maybe this will get you started.


#!/bin/sh

for file in /home/foo_bar/*.txt
do
name=`echo $file | sed -e 's/txt/TXT/g'`
mv $file $name
done



takes all files in directory foo_bar with a txt extension and makes it a TXT extension.
actually might want to change it to 's/.txt/.TXT/g'
so that it only does the extensions, but you get the idea.

someone else may come along with a better example for you. i need to go eat some waffles now.

dfx
08-21-2002, 02:23 PM
Here's a small mod of the above script.

for X in *
do
sed -e 's/windows/linux/g' < "$X" > "$X".$$ || continue
mv -f "$X".$$ "$X"
done


Takes all files in current dir (*) and replaces windows with linux in them. I bet you can do the same in perl with a script that's only 5 chars long.

Be careful with such scripts, as they're potentially dangerous. A tiny mistake can trash all your files, so always run them with backups. Did I mention I didn't test this script?