jamen_98 has asked for the wisdom of the Perl Monks concerning the following question:

I have an ASCII file about backup tapes like:

Label Location Start Time End Time NKL0289 MSL6000 Trinity: 7 18:45 20:45

What I'm looking to do is extract the 7 number which is the slot number the tape is in. I have tried several things like using substr() and grep, but I'm not so good at Perl and am struggling. I have done some perl before (long time ago) so I understand some things, but need a little help. I have tried several variations to this, but here it is:
$slotnumber=0; $dpdrivloc = "Trinity"; open(DPSLOTFILE, "< c:\\test.txt") or die "Can't open test.txt: $!"; while (<DPSLOTFILE>) { chomp; for $chunk (split) { print "$chunk\n"; if ($chunk eq $dpdrivloc) { print "We have a match folks!"; } } } #system("c:\\ipbat.bat"); close DPSLOTFILE;
It also runs a batch file which I've commented out, but that works great already. I need to extract the number after MSL6000 Trinity: and place it after the the batch file with that number as an argument. I think I can handle that part though.

Edit by BazB remove pre tags.

Replies are listed 'Best First'.
Re: Extract field after keyword
by dragonchild (Archbishop) on Mar 19, 2004 at 19:32 UTC
    while (<DPSLOTFILE>) { my ($slot) = /$dpdrivloc:\s*(\d+)/; # Do something with $slot }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Tried:
      
      
      while (<DPSLOTFILE>) { my ($slot) = /$dpdrivloc:\s*(\d+)/; print "$slot"; }
      and there was no output.... Part of me was thingking it might be an issue with an ASCII file. I tried opening the file using:
      binmode (STDOUT);
      and no difference...
        I just tried it, substituting my while-loop for yours and it works for me. Maybe posting your entire script and test file would help .....

        ------
        We are the carpenters and bricklayers of the Information Age.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        You want print "$slot\n";, since otherwise your prompt might cover things up if you don't print anything else (depending on your shell).

Re: Extract field after keyword
by Not_a_Number (Prior) on Mar 19, 2004 at 20:01 UTC

    Not sure what your input file looks like, as you only give one 'field'. However, assuming it's like the __DATA__ below (even with varying whitespace), you could do something like this:

    use strict; use warnings; my $to_find = 'Pretty Woman'; while ( <DATA> ) { if ( /$to_find:\s*(\d+)/ ) { print "Slot for $to_find: $1\n"; } } __DATA__ Label Location Start Time End Time NKL0289 MSL6000 Trinity: 7 18:45 20:45 NKL0111 MSL7000 A Fish Called Wanda: 22 11:11 13:00 XKL0ABCC MSL8000 Pretty Woman: 55 18:13 20:12

    dave

      The file is very basic... FILE:
      Report on Used Media Cell Manager: Trinity.mydomain.com Creation Date: 3/19/2004 10:05:17 AM Label Location Pool Name Protection Used [MB] + Total [MB] Last Used [NFK100L2] [MSL6000 Trinity: 7] MSL6000 None + 0.06 204800.00 3/19/2004 10:04:2
      All the code follows:
      #$slotnumber=0; $dpdrivloc = "Trinity"; open(DPSLOTFILE, "< c:\\test.txt") or die "Can't open test.txt: $!"; while (<DPSLOTFILE>) { if (/$dpdrivloc:\s*(\d+)/) { print "Slot for today is: $1\n"; } } #system("c:\\ipbat.bat"); close DPSLOTFILE;

        So, it works? (It does for me.)

        Not sure from your reply. Do you still have a problem?

        dave