Operating System - HP-UX
1754398 Members
3050 Online
108813 Solutions
New Discussion юеВ

Replace Multiple Strings on 1 file

 
SOLVED
Go to solution
Stephen Kennedy_2
Occasional Contributor

Replace Multiple Strings on 1 file

All

I am trying to perform the following now I can't use perl I need to replace multiple patterns in a file e.g

for p in 1 2 3 4
do
oldfile=`grep $p /tmp/old`
newfile=`grep $p /tmp/new`

sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
done

now I need the changes for all instances of the for loop to be saved in the 1 file any suggestions ????

6 REPLIES 6
KapilRaj
Honored Contributor

Re: Replace Multiple Strings on 1 file

If I understand correctly,U need to replace

sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2

with

sed "s%${oldfile}%${newfile}%g" FILE1 >> FILE2

Regds,

Kaps
Nothing is impossible
Stephen Kennedy_2
Occasional Contributor

Re: Replace Multiple Strings on 1 file

a double >> just causes the rest of the contents to copied to file2 for each iteration of the for loop I just need the pattern changed....
harry d brown jr
Honored Contributor

Re: Replace Multiple Strings on 1 file

Can you post what the files "oldfile" and "newfile" look like?

live free or die
harry
Live Free or Die
Stephen Kennedy_2
Occasional Contributor

Re: Replace Multiple Strings on 1 file

Example of what I am trying to achieve

I have a couple of servers in a list in a file so the for loop edits the file for each server.

Now FILE1 is the file in which the patterns are found

Script

newdate=`date`
for p in `cat servername`

do
oldfile=`grep $p FILE1`
newfile= $p$newdate

sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
done

cp FILE1 FILE.bck
mv FILE2 FILE1

FILE 1 before the for loop

CONTENT
servera.olddate
serverb.olddate
serverc.olddate
serverd.olddate
servere.olddate
Content

What I want to see

FILE 2
Content
servera.newdate
serverb.newdate
serverc.newdate
serverd.newdate
servere.olddate
Content

I want the output to look like

Now FILE1 has other content and I only need the multiple patterns changed but if I use a double >> it will direct all the contents over and over for each instance of the for loop

aaaabbbb
ccccdddd
eeeeffff


Now say I need to change patterns aaaa and cccc to new paterns xxxx and yyyy so I perform the following



harry d brown jr
Honored Contributor
Solution

Re: Replace Multiple Strings on 1 file

You need to move the "mv" into the loop and the "cp" outside the loop:



newdate=`date +'%Y%m%d%H%M%S'`
cp FILE1 FILE.bck
for p in `cat servername`
do
oldfile=`grep $p FILE1`
newfile="${p}.${newdate}"
sed "s%${oldfile}%${newfile}%g" FILE1 > FILE2
mv FILE2 FILE1
done

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Replace Multiple Strings on 1 file

or better yet, try this (SINGLE pass):


newdate=`date +'%Y%m%d%H%M%S'`
cp FILE1 FILE.bck
cat /dev/null >SED1
for p in `cat servername`
do
oldfile=`grep $p FILE1`
newfile="${p}.${newdate}"
echo "s%${oldfile}%${newfile}%g" >>SED1
done
sed -f SED1 FILE1 > FILE2
mv FILE2 FILE1

live free or die
harry
Live Free or Die