-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of Andreas Moroder
Sent: Friday, 5 January 2007 8:37 PM
To: beginners@perl.org
Subject: perl script instead of grep

Hello,

I have a directory with many files with names made like this two:

PC=laptop-fortbild,USER=fsigmund,OS=95-98
PC=INFO-FABIAN,USER=fsigmund,OS=W2K-XP

This text files may contain a line like this one

Mac-Adresse: 00:10:D7:09:5E:16

With grep

grep Mac-Adresse PC* | awk -F , '{print $1" " $3}' | awk -F = '{print 
$2" "$3}' | awk '{ print $1" "$4}'

I get a result like this
INFO-FABIAN 00:30:05:84:91:EB
laptop-fortbild 00:10:D7:09:5E:16

I need a small perl script that loops all the files and searches for 
this mac adresses. In the loop I need this two variables ( PC-Name and 
MAC ). There may be files that does not contain the MAC-Adresse line.

Can anyone please help me

Thanks
Andreas


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





Hope fully this helps 

### Script start

use strict;

my $process_dir =
'C:\work\_RHWork\projects\perl-beginers\directory-read\files-dir';

opendir(DIR,$process_dir) or die "Can't open dir: $! \n";
    my @files = readdir(DIR)  or die "Can't read dir: $! \n";
closedir(DIR);

my %macPCName;

foreach my $file (@files)
{
      next if ($file eq "."); 
      next if ($file eq "..");
      #print "File: ", $file ." \n";
      
      my @pcDetails = split/,/, $file;
      
      # Get the PC and the mac adderess if we can.. 
      if($pcDetails[0] =~ /^pc=(.+)/i)
      {
          my $pcName = $1;
          
          open(IN, $process_dir .'\\'. $file) or die "Can't open file: $file
: error $1 \n";
          while(my $line =<IN>)
          {
              if($line =~ /Mac-Adresse:\s+(.+)/i)
              {
                  #print "Mac address: [[[" . $1 . "]]] \n";
                  $macPCName{$1} = $pcName;
              }
          }
          close(IN);
          
          #print "pcdetails: $pcDetails[0] : pc name : ", $1 ."\n";
      }
}

while ( my ($key, $value) = each %macPCName) {
      # print out the pc name and mac address     
        print "$value $key\n";
 }

print "Done !!!!! \n";

exit(0);
#### Script end


Cheers 
Roman


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to