Operating System - HP-UX
1833777 Members
2254 Online
110063 Solutions
New Discussion

ksh: replacing a line in many files under a dir.

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

ksh: replacing a line in many files under a dir.

Hi,

I have a directory and it contains a lot of files. 90% of these files include a particular email address, "abc@abc.com". How do I write a ksh script to replace the address with some thing else, "xyz@xyz.com".

Thanks in advance.

Roger
none
8 REPLIES 8
Heiner E. Lennackers
Respected Contributor
Solution

Re: ksh: replacing a line in many files under a dir.

Hi,

short example:

#!/bin/ksh
cd /dir/
for i in *;do
cp $i $i.$$
sed -e 's/abc@abc.com/xyz@xyz.com/' $i.$$ >$i
rm $i.$$
done


this is just a quick hack, there are surely other and maybe better ways

Heiner
P.S. make a copy of your files before you test it.
if this makes any sense to you, you have a BIG problem
Deepak Extross
Honored Contributor

Re: ksh: replacing a line in many files under a dir.

You may have to escape the '@' to prevent it from being considered a kill character.
And if a single file can have "abc@abc.com" more than once, you may want to replace globally:

#!/bin/ksh
for i in *
do
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done
SHABU KHAN
Trusted Contributor

Re: ksh: replacing a line in many files under a dir.

Hi,

You could use perl command line for this:

cd /toyourdirectory
perl -p -i -e 's/abc\@abc.com/xyz\@xyz.com/g' *

Will replace all occurrences in the file itself, no moving of files around ...

Thanks,
Shabu
MAD_2
Super Advisor

Re: ksh: replacing a line in many files under a dir.

I know this thread already responded the question to the satisfaction of the original creator. However, how do can you keep the same ownership and access rights of the original file?

Thanks
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
MAD_2
Super Advisor

Re: ksh: replacing a line in many files under a dir.

"Do can" What is that, right? Sorry, "How can you keep the original access rights and ownership?"
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Jeff Schussele
Honored Contributor

Re: ksh: replacing a line in many files under a dir.

Hi ADAM,

Use a -p on the cp command line - EX
cp -p file1 file2
the -p preserves perms & ownership.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
MAD_2
Super Advisor

Re: ksh: replacing a line in many files under a dir.

Sorry, actually I was referring to the example like the one Deepak uses:

#!/bin/ksh
for i in *
do
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done

Wouldn't the redirection make the new files have as owner whoever runs the script?
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Darrell Allen
Honored Contributor

Re: ksh: replacing a line in many files under a dir.

Hi Adam,

Add a cp -p command just before the sed command:

#!/bin/ksh
for i in *
do
cp -p $i $i.out
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)