Link to home
Start Free TrialLog in
Avatar of Jm2005
Jm2005

asked on

Replace Strings with Strings from a file.

i'm looking to create a for loop or something similar to accomplish the following.

ive got files, one with a list of the text i want to replace.
i'll refer to this as the source file:
example:
<description>test.0</description>
<description>test.1</description>
<description>test.2</description>
etc..

the destination has similar entries, but i need to replace each line with one from the source sequentially... would a for loop do this properly?

Avatar of arnold
arnold
Flag of United States of America image

Lets see if i get what you have:

You have file1 that has:
<description>test.0</description>
<description>test.1</description>
<description>test.2</description>

You have file2 that has:
test.0 somenewname
test.1 anothername
test.2 completelynewname

After the script run you want for file1 to have:
<description>somenewname</description>
<description>anothername</description>
<description>completelynewname</description>

Is that right?

perl might be better if the above is representative of the data.
i.e. build a hash from the second file based on the name-> replacementname


Avatar of Jm2005
Jm2005

ASKER

file 2 has:

<description>somethingelse</description>
<description>somethingnotthesame</description>

i want file1's data to replace file 2's <description> tag
ASKER CERTIFIED SOLUTION
Avatar of Hugh Fraser
Hugh Fraser
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jm2005

ASKER

okay, i was mistaken:

i need to change between the descriptions in file 2

file one is the values i need to change them to, in sequential order. there is no reference between them otherwise.
Sorry if I'm overlooking the obvious, but if you take the values between the description tags in file 1 and replace the values between the description tags in file 2, don't you just have a copy of file 1, since the format of both files is the same?

Do the files have different numbers of records, which would make the resulting file different than either of the sources? If so, how do you want to resolve the differences?
Avatar of Jm2005

ASKER

i was wrong about file 2..
file 2 is just a list of values to put into file1's description tags, and they are different.

they are the same exact number of records.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jm2005

ASKER

that while read x; do echo "<description>"$x"</description>"; done <file2.txt works, but i need to replace the description tags in the files.
Avatar of Jm2005

ASKER

the second file has other data i dont want to damage.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jm2005

ASKER

ive tried this:

for i in $(cat change); do  cat tempate.xml |sed 's/<description>ifAdminStatus.*/<description>connection to '$i'<\/description>/'; done | less


but it replaces EVERY entry with the first line in change and it loops infinitely until i kill it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll re-iterate arnold's request. Please post a sample of both files. As arnold said, once we know exactly what you're trying to do, it's simple to craft a short perl program to do it.