Solved

Replace Strings with Strings from a file.

Posted on 2008-10-23
13
227 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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