更新值(查找和替换)

时间:2015-03-20 00:55:34

标签: perl shell

我需要比较和替换两个文件中的值。我有两个大约1000多个员工记录的平面文件。目标是使用列表UPDATE到RECORD的第一列中的员工ID更新电话号码。我写了一些东西......

更新记录

E0000013 8327733241X7131030001
E0011183 8327731234
E0000231 8327731873
E0000453 8321832322
E0003421 8328722221
E0000398 8327739741X1112223333

SADE MADEUP RECORD - 不是真名或数字

E0000013 MARY JACKSON 1111FANNINS ST HOUSTON TX 77004 7131030001X7131030001 FL21
E0011187 JANE DOE     1111FANNINS ST HOUSTON TX 77004 7131030003            FL17
E0000231 JUANITTA     1111FANNINS ST HOUSTON TX 77004 7131030002            FL38
E0000453 ETHA MACK    1111FANNINS ST HOUSTON TX 77004 7131030004            FL31
E0000542 EDDIE JOE    1111FANNINS ST HOUSTON TX 77004 7131030009            FL32
E0003421 FRANCIS Y    1111FANNINS ST HOUSTON TX 77004 7131030008            FL33
E0000398 ZAYLA YEN    1111FANNINS ST HOUSTON TX 77004 7131030018            FL41

CODE

请在我的回答中找到更新的代码。我试图删除一个正在改变我的一些价值观的错误。我不是想让任何人为我编写代码,而是突出我的错误并提供良好的建议。

预期结果

E0000013 MARY JACKSON 1111FANNINS ST HOUSTON TX 77004 8327733241X7131030001 FL21
E0011187 JANE DOE     1111FANNINS ST HOUSTON TX 77004 8327731234            FL17     
E0000231 JUANITTA     1111FANNINS ST HOUSTON TX 77004 8327731873            FL38
E0000453 ETHA MACK    1111FANNINS ST HOUSTON TX 77004 8321832322            FL31
E0000542 EDDIE JOE    1111FANNINS ST HOUSTON TX 77004 8321834343            FL32
E0003421 FRANCIS Y    1111FANNINS ST HOUSTON TX 77004 8328722221            FL33
E0000398 ZAYLA YEN    1111FANNINS ST HOUSTON TX 77004 8327739741X1112223333 FL41

ACTUAL-OUTPUT 正如您所看到的,实际输出正在将我的数据转移到右边的Mary Jackson和。如何摆脱空间并保持地板值不变?

E0000013 MARY JACKSON 1111FANNINS ST HOUSTON TX 77004 8327733241X7131030001            FL21
E0011187 JANE DOE     1111FANNINS ST HOUSTON TX 77004 7131030003            FL17
E0000231 JUANITTA     1111FANNINS ST HOUSTON TX 77004 8327731873            FL38
E0000453 ETHA MACK    1111FANNINS ST HOUSTON TX 77004 8321832322            FL31
E0000542 EDDIE JOE    1111FANNINS ST HOUSTON TX 77004 8321834343            FL32
E0003421 FRANCIS Y    1111FANNINS ST HOUSTON TX 77004 8328722221            FL33
E0000398 ZAYLA YEN    1111FANNINS ST HOUSTON TX 77004 8327739741X1112223333            FL41

2 个答案:

答案 0 :(得分:1)

我要指出的是,您的示例代码中充斥着对我来说非常明显的错误,而且我只有约3个月的时间。

也许请阅读free bookllama book

至于你的源代码,它不会像那样工作。我继续把它扔在一起做你想做的事,但我建议你学习更多关于perl的知识,这样你就可以更容易地解决这些问题。

#!/usr/bin/perl
use warnings;
open $newnums_fh, '<', '/home/job/newfile.txt' or die $!;
open $records_fh, '<', '/home/job/records.txt' or die $!;
open $newrecords_fh, '>', '/home/job/records_out.txt' or die $!;
while(<$newnums_fh>) {
    my @line = split(/\s+/, $_);
    my $employee_id = shift @line;
    my $employee_phone = pop @line;
    $phonenums{ $employee_id } = $employee_phone;
}
close($newnums_fh);
while(<$records_fh>) {
    my @line = split(/\s+/, $_);
    my $employee_id = shift @line;
    if(exists $phonenums{ $employee_id} ) {
        my $new_phone = $phonenums{ $employee_id };
        unshift @line, $employee_id;
        my $last_value = pop @line;
        my $old_phone = pop @line;
        push @line, $new_phone;
        push @line, $last_value;
        if(scalar @line == 9) {
            print $newrecords_fh join("\t", @line) . "\n";
        } elsif(scalar @line == 10) {
            print $newrecords_fh shift @line; #employee ID
            print $newrecords_fh "\t" . shift @line; #first name
            print $newrecords_fh ' ' . shift @line; #second name
            print $newrecords_fh "\t";
            print $newrecords_fh join("\t", @line) . "\n";
        } else {
            warn "Line is the wrong size! going to skip the entry!\n";
        }
    } else {
        next;
    }
}

答案 1 :(得分:0)

这就是我解决这个问题的方法,它按预期工作,感谢大家的观看或评论

#!/usr/bin/perl
use warnings;
use strict;

my %employeehash;

open my $RECORD,'<','/home/maryh/Documents/employee.txt' || die $!;
open my $UPDATE,'<','/home/maryh/Documents/updatedphone.txt' || die $!;
open my $MODIFIED,'>','/home/maryh/Documents/xphone.txt' || die $!;


while (<$UPDATE>) {

   my ($emp_id, $phone) = split /\s+/;

   $employeehash{$emp_id} = $phone;

}
while(<$RECORD>) {

   chomp(my $line = $_);
   my $emp_id = substr($line,0,8);
   my $phone = substr( $line ,54,10);
   $emp_id =~ s/^\s+|\s+$//g;
   $phone =~ s/^\s+|\s+$//g;

 if (exists $employeehash{$emp_id} ) {

      $emp_id = $employeehash{$emp_id};
      $emp_id =~ s/^\s+|\s+$//g; 
      substr($line,54,21) =~ s/$phone/$emp_id/; 
      print "[$phone] got change to [$emp_id]\n"; 

  }

print $MODIFIED "$line\n" ; 

}

close $MODIFIED;
close $UPDATE;
close $RECORD;