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

Hi guys, Was wondering if I could get help on the code below. I would like to go through all the *.log files in a directory and perform the following code on them. Instead of just print $3 though, I would like to print this to a new file (a new file for each log file). So i need to create lots of new files (one for each log file) and then but the $3 into it.... Thanks in advance, Matt.
@files = glob "*.log"; foreach $file (@files) { next unless $file =~ /planetlab/; print "begin\n"; local($\, $,) = ("\n", " "); while(<>) { if(/([\d:.]+).*?\bReceived.*?\bBT_PIECE.*?\bdata.*?\bPeer: (\w +):\s+(\d+\.\d+\.\d+\.\d+):\s*(\d+)/) { print $3; } } print "end"; print "$file\n"; }

Replies are listed 'Best First'.
Re: Log Parsing
by Corion (Patriarch) on Sep 07, 2007 at 12:16 UTC

    So you will need to open a new output file whenever you are processing a new input file. You will then need to change your print statement to output to the handle of the output file. That's it. Where exactly do you have problems?

      Many thanks in advance...
      @files = glob "*.log"; $count = 1; foreach $file (@files) { next unless $file =~ /planetlab/; open FILE, >"/home/imperial_crossbt/results/logging/".$count.".log +"; print FILE "begin\n"; local($\, $,) = ("\n", " "); while(<>) { if(/([\d:.]+).*?\bReceived.*?\bBT_PIECE.*?\bdata.*?\bPeer: (\w +):\s+(\d+\.\d+\.\d+\.\d+):\s*(\d+)/) { print FILE $3; } } print FILE "end"; print "$file\n"; close FILE; $count++; }
      Sorry, I'm new to file handling in perl and wasn't sure how to create files and write to them. Not sure either how to make the new files' filenames because they'd have to be say the newfile + a number that is incrementing for example (they can't all be the same). Thanks in advance for your help. Matt.
        Could I for example:
        open filehandle, >"/home/mj/newlogs"+a number+".log" print filehandle, $3 close filehandle
        Not sure how to get the number in there...
      $count = 0; open filehandle, >"/home/mj/newlogs"+$count+".log" print filehandle, $3 close filehandle $count++;
      Something like that?? Thanks in advance.
        No.
        open filehandle, ">", "/home/mj/newlogs$count.log"; # or open filehandle, '>', '/home/mj/newlogs/ . $count . '.log';
        Additionally you should check if open succeeded: open filehandle, ">", "/home/mj/newlogs$count.log" or die "Can't write to file: $!";

        (Update: fixed typo spotted by Corion++)

        Just for completeness, this doesn't do what you think it does:
        print filehandle, $3

        To print to a filehandle use print FILEHANDLE VALUES. Notice the lack of a comma between FILEHANDLE and VALUES. The correct version of your print would be:

        print filehandle $3