in reply to Log Parsing

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?

Replies are listed 'Best First'.
Re^2: Log Parsing
by mattwortho (Acolyte) on Sep 07, 2007 at 13:02 UTC
    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++; }
Re^2: Log Parsing
by mattwortho (Acolyte) on Sep 07, 2007 at 12:28 UTC
    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.
Re^2: Log Parsing
by mattwortho (Acolyte) on Sep 07, 2007 at 12:42 UTC
    $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++)

        @files = glob "*.log"; $count = 1; foreach $file (@files) { next unless $file =~ /planetlab/; open FILE, ">", "/home/mj/ipAddresses$count.log" or die "Can't wri +te to file: $!"; 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++; }
      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