#!/usr/bin/perl use warnings; use strict; my $tmpipaccountingfile = "tmpipaccountingfile"; my $DestDevice = "Box"; my %data; open my $TMPIPACCOUNTINGFILE, "<", "$tmpipaccountingfile" or die "$!"; while (<$TMPIPACCOUNTINGFILE>) { next if (/^(?:$|sh|\s*Source|^$DestDevice|Accounting)/); my @fields = split; push @{ $data{$fields[1]} }, [ @fields[0,2,3] ]; } close $TMPIPACCOUNTINGFILE; while () { print; chomp; for my $match (@{ $data{$_} }) { print join "\t", $match->[0], $_, @{$match}[1,2]; print "\n"; } } __DATA__ 10.2.9.2 10.2.9.3 10.2.9.6