?
Solved

Replace Strings with Strings from a file.

Posted on 2008-10-23
13
Medium Priority
?
230 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 79

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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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 79

Assisted Solution

by:arnold
arnold earned 1000 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 79

Assisted Solution

by:arnold
arnold earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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