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

I have a log file as below;
show tech-support details -------------------- show switchname ------------------ CCC216_MX_RED ****************************************************************** -------------------- show interface mgmt0 ------------------ mgmt0 is up Hardware is FastEthernet Address is 000f.3494.9a0c Internet address is 10.3.811.51/20 MTU 1500 bytes, BW 100 Mbps half Duplex 67992323 packets input, 2147483647 bytes 0 multicast frames, 0 compressed 0 input errors, 0 frame, 0 overrun 0 fifo 18569093 packets output, 1762088350 bytes, 0 underruns 0 output errors, 29925 collisions, 281 fifo 10686 carrier errors ****************************************************************** -------------------- show version ------------------ Cisco Storage Area Networking Operating System (SAN-OS) Software TAC support: http://www.cisco.com/tac Copyright (c) 2002-2004, Cisco Systems, Inc. All rights reserved. The copyrights to certain works contained herein are owned by other third parties and are used and distributed under license. Some parts of this software are covered under the GNU Public License. A copy of the license is available at http://www.gnu.org/licenses/gpl.html. Software BIOS: version 1.1.0 loader: version 1.2(2) kickstart: version 2.0(3) system: version 2.0(3) ******************************************************************
I need to read the file into an array where where each elemnt of the array is a chunk of data between a ------ and *****

I tried reading the whole file into a $data and then split it like so
#! c:/perl/bin/perl.exe use strict; open (LST,"c:/test/showtech215.txt")||die "$^E ; $!\n"; chomp (my $data = <LST>); print "$data" #my @array = split /\*+/,$data;
but the $data only contained the first line of the log file!

Any idea how can I achieve this?

Regards
Blackadder

Replies are listed 'Best First'.
Re: Spliting a log file into chunks and saving it into an array
by Zaxo (Archbishop) on Sep 15, 2005 at 09:47 UTC

    This should do it,

    my @array = do { local $/ = ('*' x 66) . "\n"; my @tmp = <>; chomp @tmp; @tmp; };
    That will leave the "-----" lines present. They can be removed with substr/index or a regex substitution.

    substr $_, 0, 2 + length("\n") + index($_, "--\n"), '' for @array;

    After Compline,
    Zaxo

Re: Spliting a log file into chunks and saving it into an array
by Samy_rio (Vicar) on Sep 15, 2005 at 09:27 UTC

    Hi, use "undef $/".

    #! c:/perl/bin/perl.exe use strict; undef $/; open (LST,"c:/test/show.txt")||die "$^E ; $!\n"; my $data = <LST>; #print "$data"; my @array = split(/\*+/, $data); print "$_\n" for (@array);

    I think it will help you.

    Regards,
    Velusamy R.

      This will work, but a word of advice from someone bitten by it before: save off that $/ variable first!

      #! c:/perl/bin/perl.exe use strict; $tmp_eol=$/; undef $/; . . .
      That way, when you are done splitting the chunks, you can take each array element and split them along $/ as you normally would do so.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

        That's what local is for ;-)

        my $line = do { local $/; <LST>; };

Re: Spliting a log file into chunks and saving it into an array
by Hue-Bond (Priest) on Sep 15, 2005 at 20:09 UTC

    I'm surprised none of the replies so far used the range operator. I think it's the right tool for the job. This code is tested and working on Linux:

    my $chunk = 0; my @data; open my $infd, '<', 'foo' or die "open: $!"; while (<$infd>) { next unless (my $c = /^-+/ .. /^\*+$/); next if ($c == 1); $chunk++, next if ($c =~ /E0$/); $data[$chunk] .= $_; } close $infd; ## show results for (0..$chunk-1) { print "-- beginning of chunk $_ --\n", $data[$_], "-- end of chunk $_ --\n"; }

    Don't forget to s|foo|c:/test/showtech215.txt|; and s|(\$!)|\$^E $1|;.

    --
    David Serrano

Re: Spliting a log file into chunks and saving it into an array
by Anonymous Monk on Sep 15, 2005 at 12:52 UTC
    Your $data contained the first line of your logfile because you used the diamond operator in a scalar environment. So only the first line was read into your variable. my @array = <LST>; would read every line into a separate array segment. Just to make clear why you had only one line of content. For the solution of your problem you got already some advice from my predeccessors.