How can I match and sum records from a file using Perl?
I have a text file in the following format:
211B1 CUSTOMER|UPDATE|
211B2 CUSTOMER|UPDATE|
211B3 CUSTOMER|UPDATE|
211B4 CUSTOMER|UPDATE|
211B5 CUSTOMER|UPDATE|
567FR CUSTOMER|DELETE|
647GI CUSTOMER|DELETE|
I want the script to process a text file and report the following:
- "UPDATE" for CUSTOMER column found for Acct's: 211B1,211B2,211B3,211B4,211B5
- "DELETE" for CUSTOMER column found for Acct's: 5675FR, 6470GI
I can script simple solutions, but it seems a bit tricky to me, and I would appreciate help or guidance.
collate.pl
#!/usr/bin/perl
use strict;
my %actions;
while (<>) {
my ($key, $fld, $action) = /^(\w+) (.+?)\|(.+?)\|/ or die "Failed on line $.!";
push @{$actions{$action}{$fld}}, $key;
}
foreach my $action (keys %actions) {
foreach my $fld (keys %{$actions{$action}}) {
print "\"$action\" for column $fld found for Acct's: " . join(",", @{$actions{$action}{$fld}}), "\n";
}
}
Use like this:
perl collate.pl < input.txt > output.txt
source to share
With awk:
echo '211B1 CUSTOMER|UPDATE|
211B2 CUSTOMER|UPDATE|
211B3 CUSTOMER|UPDATE|
211B4 CUSTOMER|UPDATE|
211B5 CUSTOMER|UPDATE|
567FR CUSTOMER|DELETE|
647GI CUSTOMER|DELETE|' | awk -F '[ |]' '
BEGIN {
upd="";del=""
} {
if ($3 == "UPDATE") {upd = upd" "$1};
if ($3 == "DELETE") {del = del" "$1};
} END {
print "Updates:"upd; print "Deletes:"del
}'
gives:
Updates: 211B1 211B2 211B3 211B4 211B5
Deletes: 567FR 647GI
Basically it just splits each line into three fields (with an option -F
) and maintains a list of updates and deletions that it joins to, depending on the "command".
BEGIN
and END
run before and after all string processing, so they get initialized and final output.
I would put it in a script to make it easier. I left it as a command line tool since I usually debug my awk scripts.
source to share
#!/usr/bin/perl
use strict;
use warnings;
my %data;
while ( my $line = <DATA> ) {
next unless $line =~ /\S/;
my ($acct, $col, $action) = split /\s|\|/, $line;
push @{ $data{$action}->{$col} }, $acct;
}
for my $action ( keys %data ) {
for my $col ( keys %{ $data{$action} } ) {
print qq{"$action" for column $col found for acct's: },
join q{,}, @{ $data{$action}->{$col} }, "\n";
}
}
__DATA__
211B1 CUSTOMER|UPDATE|
211B2 CUSTOMER|UPDATE|
211B3 CUSTOMER|UPDATE|
211B4 CUSTOMER|UPDATE|
211B5 CUSTOMER|UPDATE|
567FR CUSTOMER|DELETE|
647GI CUSTOMER|DELETE|
source to share
Based on your question, you can do this:
perl -i.bak -pe'if(/^211B[1-5]/){s/CUSTOMER/UPDATE/}elsif(/^(5675FR|6470GI)/){s/CUSTOMER/DELETE/}' filename
Although now I notice that the last two account numbers are different in this example, and also that the second column already has these values ββ...
source to share