Solved

Replace Strings with Strings from a file.

Posted on 2008-10-23
13
228 Views
Last Modified: 2012-05-05
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?

0
Comment
Question by:Jm2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 78

Expert Comment

by:arnold
ID: 22787057
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


0
 

Author Comment

by:Jm2005
ID: 22787213
file 2 has:

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

i want file1's data to replace file 2's <description> tag
0
 
LVL 12

Accepted Solution

by:
hfraser earned 250 total points
ID: 22788002
From what you've described, it looks like file 1 is identical to file 2 except for what's beween the description tags. If it's a line-by-line substitution, then the result of substituting the content in file 1 into file 2 is the same as file 1. So I suspect there's a key not shown that links a line in file 1 to a line in file 2, as arnold has suggested, and that it's a keyed substitution, not a sequential one.

More info is needed.
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

Author Comment

by:Jm2005
ID: 22788121
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.
0
 
LVL 12

Expert Comment

by:hfraser
ID: 22788442
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?
0
 

Author Comment

by:Jm2005
ID: 22788711
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.
0
 
LVL 12

Assisted Solution

by:hfraser
hfraser earned 250 total points
ID: 22788931
It's simpler to just read file2 and write out the contents with the description tags wrapped around it. The first file isn't really important in this scenario. Is this what you want?

while read x; do echo "<description>"$x"</description>"; done <file2.txt
0
 

Author Comment

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

Author Comment

by:Jm2005
ID: 22789471
the second file has other data i dont want to damage.
0
 
LVL 78

Assisted Solution

by:arnold
arnold earned 250 total points
ID: 22789840
Could you post a sample record from file1 and a sample from file2?
Perl is still the simplest.
1) open both files.
2) assign the first line of file2 to a variable.
3) go through line by line in file1 until you hit a description tag. replace the entry with the content of the variable from step 2 (if the entries are enclosed by the description tags, it makes the replacement part simpler.) The variable now needs to point to the next line of file2.

I think this will take less then ten lines in perl or bash.
Multi file read in bash:
http://lists.evolt.org/archive/Week-of-Mon-20040412/157907.html
0
 

Author Comment

by:Jm2005
ID: 22789916
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.
0
 
LVL 78

Assisted Solution

by:arnold
arnold earned 250 total points
ID: 22790198
You have to open both files.
cat will run as many times as there are lines in the change file.

i.e. you will first have each description line from template.xml replaced by the first line.
then by the entire template.xml will have the second line replace all the description tags.
.
.
.
.
until the last line of change has replaced all the description tags in template.xml.

i.e. if change has 5 lines, the output will be the combined length of five template.xml.
0
 
LVL 12

Expert Comment

by:hfraser
ID: 22792040
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.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question