WARNING: Untested, incomplete code ahead
I would create this script in form of a filter:
#!/usr/bin/perl -i.bak
:
and would make the file_a an option so that you can do it like this:
$ ./myfilter -i file_a file_b
This would then create a new file_b and preserve a backup file_b.bak.
First thing you should do in the filter is find all the things you'd like to replace in file_b (should it be more than just one) and store it in a hash:
while (<filea>) {
if ( this is a line i want ) {
s/[\015\012]+$//; # Just to remove any CR/LF
my($router,$site)= split /,/,$_,3;
$replace{$router}= $site;
}
}
my $replacements= '\b('.join('|',keys %router).')\b';
After that you have a list of texts to search for as
keys %replace and the replacement as
$replace{...}. You also have a string of the form:
\b(router1|router2...|routerN)\b that will be used in a regular expression.
When you've done with that, simply read and write what ever comes through
<> replacing what you need:
my $symbol;
while (<>) {
if (my $hit/^SYMBOL: $replacements/i ... /^END SYMBOL/i) {
if ($hit==1) {
$symbol=$1;
}
else {
s/^(Parent Submap:).*/$1 $replace{$symbol}/;
}
}
print;
}
That's it.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.