Solved

Replace Strings with Strings from a file.

Posted on 2008-10-23
13
223 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 76

Expert Comment

by:arnold
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Assisted Solution

by:hfraser
hfraser earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
the second file has other data i dont want to damage.
0
 
LVL 76

Assisted Solution

by:arnold
arnold earned 250 total points
Comment Utility
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
Comment Utility
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 76

Assisted Solution

by:arnold
arnold earned 250 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 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

17 Experts available now in Live!

Get 1:1 Help Now