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

Hi, I am trying to write a script that will open up a log, grab some strings in it, and print out a new log containing those strings. I want it to grab a whole string that starts with "_ and ends with ". Here's what I have so far:
use strict; use warnings; my $out = "Backup/backup.txt"; my $logfile="log.txt"; my $MAL = "\"_"; my $endstring = "\""; my $logfile_new; open LOG, "<", $logfile or die "Cannot open $logfile for read :$!"; open OUT, ">$out" or die "Cannot open $out for write :$!"; while ( <LOG> ) { if ( $_ =~ /$MAL/i ) { # Grab all strings that contain this regex # Save them all in $out } } print OUT "$out";
Any help is much appreciated!

Replies are listed 'Best First'.
Re: script to grab string from log and print them in a different log
by liverpole (Monsignor) on Feb 01, 2007 at 16:03 UTC
    Hi perl_geoff,

    It looks like the only thing left to do is print the matching lines to the output file:

    while ( <LOG> ) { if ( $_ =~ /$MAL/i ) { # Grab all strings that contain this regex # Save them all in $out print OUT $_; }

    Note that there's no comma "," after the file descriptor OUT.

    Does that do what you want?

    Update:  You don't need the final print OUT "$out"; of course.  Also, since the =~ operator works on $_ by default, you could simplify the line containing the regex match to:  if (/$MAL/) {.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks, that helps but I am not sure how to write the logic for my commented out sudo code: # Grab all strings that contain this regex # Save them all in $out Thanks :)
        Oh, sorry ... didn't see that! :-/

        Try this:

        # Find lines starting with "_ and ending with ". my $MAL = '^"_.*"$';

        Read up on perlretut for a good beginning guide to regular expressions.


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: script to grab string from log and print them in a different log
by davorg (Chancellor) on Feb 01, 2007 at 16:15 UTC
    use strict; use warnings; my $out = "Backup/backup.txt"; my $logfile="log.txt"; # Single quotes for more aethestically pleasing strings my $MAL = '"_'; my $endstring = '"'; # Build a regular expression my $re = qr[($MAL.*?$endstring)]; open LOG, "<", $logfile or die "Cannot open $logfile for read :$!"; open OUT, ">", $out" or die "Cannot open $out for write :$!"; while (<LOG>) { # Grab all strings that contain this regex if (/$re/) { # Save them all in $out print OUT $1; } }
Re: script to grab string from log and print them in a different log
by Moron (Curate) on Feb 01, 2007 at 17:32 UTC
    This example shows a regexp that checks for both patterns.
    use strict; use warnings; my $out = "Backup/backup.txt"; my $logfile="log.txt"; open LOG, "<$logfile" or die "Cannot open $logfile for read :$!"; open OUT, ">$out" or die "Cannot open $out for write :$!"; /\"_.*\"/ && print OUT $_ while( <LOG> ); close LOG; close OUT;
    or if you mean only print what's between the delimiters:
    /\"_(.*)\"/ && print OUT "$1\n" while ( <LOG> );

    -M

    Free your mind

Re: script to grab string from log and print them in a different log
by roboticus (Chancellor) on Feb 02, 2007 at 03:19 UTC
    perl geoff:

    You might also consider not writing any code at all: Good ol' grep could do it in a jiffy.

    grep '\"_' log.txt >Backup/backup.txt

    --roboticus