replace string in multiple files

Hi, I'm new to Unix. My understanding of Unix and its command is very limited.

I have about 1000 text files that have a word in it that I need to replace with a different word.

e.g.

a.txt has 1 line of txt: monday, tuesday, wednesday
b.txt has 1 line of txt: monday, tuesday, wednesday
c.txt has 1 line of txt: monday, tuesday, wednesday

I want to change all file contents to: monday, friday, wednesday

Is there a script I can create to do this? Thanks

hey millsy, my knowledge is fairly limited as I don't use Unix much any more. 1 question - do you have Perl?
Anyway I will assume do don't . There is a command in Unix called sed. This should do what you want. Here's an example I copied off the web. I haven't checked it out but at a glance it looks like it will do what you want.

for file in $(grep -il "tuesday" *.txt)
do
sed -e "s/tuesday/friday/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done

The grep -il statement finds all instances of tuesday in files ending in txt (i = ignore case, l = only list the filename). The file names are passed to sed, which runs a regular expression to change all instances of tuesday to friday. Since sed doesn't overwrite a file, I redirected the output to a temp file and then renamed it back to the original file name.

Hope this helps

hi frank, i tried that and got the following error:

replace_script: syntax error at line 2: `$' unexpected

do you know why i am getting this syntax error???

replace_script is the name of my text file that contains the code you gave me.

no i don't have perl.

$ for i in *.txt ;do nawk '/tuesday/ {sub(/tuesday/, "friday")};1' $i > ${i}_tmp ; mv ${i}_tmp $i; done

hey j, its not finding i. does this need to be declared?

No need to declare the i .. What is the error u got when fired that code ..