How about this? (I tried to made it simple and readable, instead of terse. But ask if you do not understand a part, or do not know how to extend it to what you need)

#!/usr/bin/perl use strict; use warnings; my @FILENAMES_ARP = <./*arp*.txt>; my @FILENAMES_MAC = <./*mac*.txt>; # use File::Slurp; this way this works: my @LINES =read_file($FILE + ); my %MACS; my $FILE; my $CLEAN_ARP_REC = './clean_arp.tmp'; open (OUTPUT, ">", $CLEAN_ARP_REC) or die "Could not open $CLEAN_ARP_R +EC because $!"; for $FILE (@FILENAMES_MAC){ print "processing $FILE...\n"; if( open(FH, "<", $FILE) ){ while(<FH>){ chomp; next unless /^\*/; # skip if line does not start with "*", + for example, empty lines next if /static/; # skip static entries my @F = split(/\s+/, $_); my $VLAN = $F[1]; my $MAC =$F[2]; my $INT = $F[7]; next unless $MAC=~/^[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}$ +/; if($MACS{$MAC}){ warn "$MAC already defined, skipping entry at line $.\ +n"; }else{ $MACS{$MAC}{VLAN}=$VLAN; $MACS{$MAC}{INT}=$INT; } } close FH; }else{ warn "Could not open $FILE, skipping. Errormsg=$!\n"; } } for $FILE (@FILENAMES_ARP){ print "Processing $FILE...\n"; if( open(FH, "<", $FILE) ){ while(<FH>){ chomp; my @F = split(/\s+/, $_); my $MAC = $F[3]; my $VLAN = $F[5]; my $IP = $F[1]; next unless $IP; # must be defined? if($MACS{$MAC}){ $MACS{$MAC}{IP}=$IP; # add this extra data $MACS{$MAC}{_}=1; # have to "merge" } } close FH; }else{ warn "Could not open $FILE, skipping. Errormsg=$!\n"; } } for my $MAC (sort keys %MACS){ if( defined $MACS{$MAC}{_} ){ print OUTPUT $MAC . ", " . $MACS{$MAC}{IP} . ", " . $MACS{$MAC}{VLAN} . ", " . $MACS{$MAC}{INT} ."\n"; }else{ # normal line, do not print ? } } close OUTPUT;

In reply to Re^3: Merging hashes at key match by FreeBeerReekingMonk
in thread Merging hashes at key match by GeorgMN

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.