On Tue, 14 Aug 2012 11:50:14 +0100
jet speed <speedj...@googlemail.com> wrote:

> i have an @newfa with 50:00:00:08:44:9a:fb:79:90
> 50:00:00:90:44:9a:fb:30:90 50:00:00:55:44:9a:fb:79:66 and a file as
> below. now i want check if the @newfa entry present in the file if it
> exists then i need to print the matching zone: name
> 
> 
> zone:            tstdetdr_B_AARA_30767_CL45
>                      10:00:00:00:c7:d5:cd:16:c6
>                       50:00:00:08:44:9a:fb:79:90
> zone:            artdetdr_B_AARA_30767_CL45
>                      10:00:00:00:c7:d5:cd:16:c9
>                       50:00:00:08:44:9a:fb:64:90
> zone:            dbtdetdr_B_AARA_30767_CL45
>                      10:00:00:00:c7:d5:cd:16:ca
>                       50:00:00:08:44:9a:fb:74:90

Try:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# --------------------------------------
# Configuration

my $key_file  = 'file.txt';
my $zone_file = 'zone.txt';

# --------------------------------------
# Working storage

# create a hash for the keys
my %keys = ();

# --------------------------------------
#       Name: load_keys
#      Usage: load_keys();
#    Purpose: Load the keys into %keys
# Parameters: (none)
#    Returns: (none)
#
sub load_keys {

  open my $key_fh, '<', $key_file or die "could not open $key_file:
  $!\n";

  while( <$key_fh> ){
    chomp;
    s/(..)/:$1/gs;
    s/^://;
    $keys{ lc($_) } = 1;
  }

  close $key_fh or die "could not close $key_file: $!\n";

  # for debugging
  # print Dumper \%keys;

  return;
}

# --------------------------------------
#       Name: scan_zone
#      Usage: scan_zone();
#    Purpose: scan the zone file and report the name if the key is
present # Parameters: (none)
#    Returns: (none)
#
sub scan_zone {

  open my $zone_fh, '<', $zone_file or die "could not open $zone_file:
  $!\n";

  my $zone_name;
  while( <$zone_fh> ){
    chomp;

    # get the zone name
    if( /zone\:/ ){
      $zone_name = $_;
      next;
    }

    # check for key
    s/\s+//g;
    if( exists $keys{ lc($_) } ){
      print "$zone_name\n";
    }
  }

  close $zone_fh or die "could not close $zone_file: $!\n";

  return;
}

# --------------------------------------
# main

# read the keys
load_keys();

# scan the zone file and report the name if the key is present
scan_zone();

__END__


-- 
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

        _Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help    : http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news            : http://perlsphere.net/
repository      : http://www.cpan.org/
blog            : http://blogs.perl.org/
regional groups : http://www.pm.org/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to