我有两个文件,如下所示。
- 地址文件 [查找文件]
POST OFFICE,PO SUITE ACCESS ROOM, SAR SUITE,STE STREET,ST NEW YORK,NY POST,PST LONG LINE STREET,LLS
- 主文件[需要查找和替换确切字符串的地方]
ARIJIT, 192 POST OFFICE, SUITE CHANDA, 13 HP STREET, NY RAM, POSTING POST, LONG LINE STREET ROY, POST 3009, SUITE ACCESS ROOM
预期输出:
ARIJIT, 192 PO, STE CHANDA, 13 HP ST, NEW YORK RAM, POSTING PST, LLS ROY, PST 3009, SAR
我正在使用下面的代码,但仍然无法获得任何成功。我是 PERL 的新手。该代码适用于单个单词,但不适用于多个单词。
#!/usr/bin/perl
use warnings;
use strict;
open( my $out_fh, ">", "output.txt" ) || die "Can't open the output file for writing: $!";
open( my $address_fh, "<", "Address.txt" ) || die "Can't open the address file: $!";
my %lookup = map { chomp; split( /,/, $_, 2 ) } <$address_fh>;
open( my $file_fh, "<", $ARGV[0] ) || die "Can't open the file.txt file: $!";
while (<$file_fh>) {
my @line = split;
for my $char ( @line ) {
( exists $lookup{$char} ) ? print $out_fh "$lookup{$char} " : print $out_fh "$char ";
}
print $out_fh "\n";
}