1753796 Members
6866 Online
108799 Solutions
New Discussion юеВ

Re: shell script help!!

 
mw_4
Frequent Advisor

shell script help!!

Hi guys.
about 500 files exists
and I have to insert strings to those files..

How can I replace the strings all files..

See attached files..
Step by step
4 REPLIES 4
Zach Parker
Occasional Advisor

Re: shell script help!!

Have a for loop to list all of the files you want to edit. In the for loop, run sed to add the syntax and redirect the output. Then rename the redirected output to the file you want.

Something like this...
(NOTE, I would reccommend testing before running. I am operating off of memory, which is falible.)

for file in `ls *.db`
do
sed '/\@ IN SOA/{x;s/^\$TTL 86400/;G;}' $file > $file.new
done

If you are brave, you could tuck in a move statement to replace the old files with the new one in the for loop. I would suggest making sure that the output is what you expect before doing so, however.

Like I said before. I am doing this from memory, so test the sed command against a sample file and redirect the output someplace neutral before running the command against all 500 files.

Hope this helps.


Zach Parker
Occasional Advisor

Re: shell script help!!

Whoops, typo, should be another forward slash:
sed '/\@ IN SOA/{x;s/^/\$TTL 86400/;G;}' $file > $file.new

A quick Google check confirms that the syntax should work.
SHABU KHAN
Trusted Contributor

Re: shell script help!!

Hi,

I would use the same for loop

but I would use a combination of sed and tr :

for inputfile in `ls *.txt`
do
sed 's/\@ IN SOA/\$TTL 86400=&/g' ${inputfile} | tr "=" "\n" > ${inputfile}.out
done

Thanks,
Shabu
H.Merijn Brand (procura
Honored Contributor

Re: shell script help!!

perl -pi -e '/^\@ IN SOA/&&s/^/$TTL 86400\n/' *
Enjoy, Have FUN! H.Merijn