Operating System - HP-UX
1836496 Members
3951 Online
110101 Solutions
New Discussion

Re: multi file substitution script

 
SOLVED
Go to solution
massimo_38
Advisor

multi file substitution script

Hi everybody.

Does anybody can suggest me a shell script (or awk) to make automatic substitution in many files?

For Example:

I have many text files (shell script) with the command "SUIVI/command" and I want to change the string in EVERY file to make it become "SUIVI/xxx/command" or something similar.

Can anybody help me?

thanks in advance.
8 REPLIES 8
Karthik S S
Honored Contributor

Re: multi file substitution script

You can use sed for this.

Something like this,

for file in `file1 file2 ...`
do
sed s/SUIVI\/command/SUIVI\/xxx\/command/g > $file.new
mv $file $file.org
mv $file.new $file
done

I have not tried this though ;-)

-KarthiK S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Mark Grant
Honored Contributor

Re: multi file substitution script

Karthik did a typo,

The line

sed s/SUIVI\/command/SUIVI\/xxx\/command/g > $file.new

should be

sed "s/SUIVI\/command/SUIVI\/xxx\/command/g" $file > $file.new
Never preceed any demonstration with anything more predictive than "watch this"
David Burgess
Esteemed Contributor

Re: multi file substitution script

Give this a try :-

#cat test | sed s/SUIVI\\/command/SUIVI\\/xxx\\/command/g > test.out
#mv test.out test

You could then put it in a loop :-

for file in test1 test2 test3
do
cat $file | sed s/SUIVI\\/command/SUIVI\\/xxx\\/command/g > $file.out
mv $file.out $file
done

Regards,

Dave.
Mark Grant
Honored Contributor

Re: multi file substitution script

How about a really quick one

perl -pi -e "s/SUIVI\/command/SUIVI\/xxx\/command" file*

The xxx could be replaced with a shell variable if you put this in a script and it's useful for you to do that. The file* is a list of files.
Never preceed any demonstration with anything more predictive than "watch this"
massimo_38
Advisor

Re: multi file substitution script

Thansk to all....I'm trying the perl command for now...

It gives me this error:

Substitution replacement not terminated at -e line 1.

What am I doing wrong?
Mark Grant
Honored Contributor

Re: multi file substitution script

You are doing nothing wrong. I did though.

The line should read

perl -pi -e "s/SUIVI\/command/SUIVI\/xxx\/command/" file*

I missed out that last "/" - sorry
Never preceed any demonstration with anything more predictive than "watch this"
Karthik S S
Honored Contributor
Solution

Re: multi file substitution script

Change the command to,

perl -pi -e "s/SUIVI\/command/SUIVI\/xxx\/command/g" file*

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
massimo_38
Advisor

Re: multi file substitution script

thanks to all!
You resolved my problem.

I used the perl command and the sed command in automatic batch...

Now I submit points.

By!