Solved

Replace Strings with Strings from a file.

Posted on 2008-10-23
13
224 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
  • 6
  • 4
  • 3
13 Comments
 
LVL 77

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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 77

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 77

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now