in reply to How to Extract Data from Text file 1 based on the Value from Text file 2

#!/usr/bin/perl use strict; use warnings; my $cust_file = <<'CUST'; 200612:ABC 200905:DEF 200212:GHI CUST # create customer ID => name lookup table my %cust_id2name; open my $cust_fh, "<", \$cust_file or die $!; while (<$cust_fh>) { chomp; my ($id, $name) = split /:/; $cust_id2name{$id} = $name; } # extract data from billing file # and store indexed by customer name my %report; while (<DATA>) { my ($cust_id, $usage) = (split ' ')[0,-1]; my ($switch) = /(SWITCH FastEthernet.*(?:IN|OUT))/; $switch =~ s/FastEthernet/Fa/; my $cust_name = $cust_id2name{$cust_id}; push @{$report{$cust_name}}, "$switch $usage" unless $usage == 0; } # print report for my $cust (sort keys %report) { print "CUSTOMER NAME:$cust\n"; print "Device_Code Port Traf_Dir Data_Usage\n"; print "$_\n" for @{$report{$cust}}; print "\n"; } __DATA__ 200612 200612 SWITCH FastEthernet1_0_33 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 4.336 1.311 200612 200612 SWITCH FastEthernet1_0_33 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 35.435 10.716 200612 200612 SWITCH FastEthernet1_0_35 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 167.976 50.796 200612 200612 SWITCH FastEthernet1_0_35 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 26.064 7.882 200612 200612 SWITCH FastEthernet2_0_33 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 -0.000 -0.000 200612 200612 SWITCH FastEthernet2_0_33 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 -0.000 -0.000 200612 200612 SWITCH FastEthernet2_0_35 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 -0.000 -0.000 200612 200612 SWITCH FastEthernet2_0_35 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 -0.000 -0.000 200905 200905 SWITCH FastEthernet1_0_48 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 3.193 0.965 200905 200905 SWITCH FastEthernet1_0_48 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 41.499 12.549 200905 200905 SWITCH FastEthernet2_0_48 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 -0.000 -0.000 200905 200905 SWITCH FastEthernet2_0_48 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 -0.000 -0.000 200212 200212 SWITCH FastEthernet1_0_19 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 3.877 1.172 200212 200212 SWITCH FastEthernet1_0_19 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 0.837 0.253 200212 200212 SWITCH FastEthernet2_0_19 OUT 01-Feb-2010_02:00 01-Mar-2 +010_02:00 -0.000 -0.000 200212 200212 SWITCH FastEthernet2_0_19 IN 01-Feb-2010_02:00 01-Mar-20 +10_02:00 -0.000 -0.000

Output:

CUSTOMER NAME:ABC Device_Code Port Traf_Dir Data_Usage SWITCH Fa1_0_33 OUT 1.311 SWITCH Fa1_0_33 IN 10.716 SWITCH Fa1_0_35 OUT 50.796 SWITCH Fa1_0_35 IN 7.882 CUSTOMER NAME:DEF Device_Code Port Traf_Dir Data_Usage SWITCH Fa1_0_48 OUT 0.965 SWITCH Fa1_0_48 IN 12.549 CUSTOMER NAME:GHI Device_Code Port Traf_Dir Data_Usage SWITCH Fa1_0_19 OUT 1.172 SWITCH Fa1_0_19 IN 0.253

(Of course, you should open your original files instead of __DATA__, etc.)