in reply to Searching and replacing text
and would make the file_a an option so that you can do it like this:#!/usr/bin/perl -i.bak :
This would then create a new file_b and preserve a backup file_b.bak.$ ./myfilter -i file_a file_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.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';
That's it.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; }
|
|---|