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

Hi All, I wrote a following script

1. To read the file and grep the scpecific line.

My requirement is to if that line exceeds it should print that information.
#C:/Dwimperl/perl/bin #To Open the file open (MYFILE,"C:/Users/katragas/Desktop/apadmin_get_status_log.txt"); if (grep {/Response Queue Size: 0/} <MYFILE>){ print "Yes, it is\n"; }else{ print "nope, it's not\n"; } close (MYFILE)
I am sure, I am doing mistake here. Can some one help to get it moving.

Replies are listed 'Best First'.
Re: I wrote a script to grep
by AppleFritter (Vicar) on Jul 22, 2014 at 11:28 UTC
      Thanks for reply. I require following: I want the script to check the file for string "Response Queue Size: 0", and if this string has other than numerical 0, I want this to be print to my screen.

        Your specification and your code don't match up very well. You say you want to look for 'Response Queue Size:' followed by a number, and then to print something if that number is not zero. What your code is actually doing is printing whether or not that specific string is found. If the string isn't found, your code gives the same message as if it is present with a different number.

        This will slurp the whole file, which is fine for small files. It will tell you if the string is not found or if the number is not zero.:

        my $fn = 'C:/Users/katragas/Desktop/apadmin_get_status_log.txt'; open my $my_file, '<', $fn or die "Cannot open $fn: $!\n"; $/ = \0; $_ = <$my_file>; close $my_file; if ( /Response Queue Size: (\d+)/ ) { my $size = $1; print "size: $size\n" if $size ne '0'; } else { print "'Response Queue Size' not found.\n"; }

        This will read the file line by line, which will save memory for larger files. It will tell you if the queue size is non-zero at the time it is found, and will tell you a total number of matches for 'Response Queue Size:' at the very end.

        my $fn = 'C:/Users/katragas/Desktop/apadmin_get_status_log.txt'; open my $my_file, '<', $fn or die "Cannot open $fn: $!\n"; my $found = 0; while ( <$my_file> ) { if ( /Response Queue Size: (\d+)/ ) { $found++; my $size = $1; print "\nsize: $size\n" if $size ne '0'; } } print "'Response Queue Size' found $found times\n"; close $my_file;

        In neither of the above is it necessary to close the file handle since there's not much left to do after the read. However, the close illustrates where the file is no longer needed in each.

        I've introduced error handling for not being able to open the file. I've also switched to three-argument open which is better for anything longer than a one-liner. I've also switched to lexical file handles rather than barewords.

        Perl is more than a fancy wrapper around regular expressions. The capture and ne test are more natural to the problem than trying to solve the whole thing as a regex.

        Update: used my to make $my_file a lexical as touched on by Laurent_R

Re: I wrote a script to grep
by davido (Cardinal) on Jul 22, 2014 at 14:47 UTC

    I want the script to check the file for string "Response Queue Size: 0", and if this string has other than numerical 0, I want this to be print to my screen.

    open my $infile, '<', 'C:/Users/katragas/Desktop/apadmin_get_status_lo +g.txt' or die $!; while( <$infile> ) { if( /Response Queue Size: (\d+)/ ) { print unless $1 == 0; } }

    Your specification is fairly clear now; you've outlined the criteria. The next step is to implement those criteria:

    • Iterate over the lines of the file.
    • If a line matches "Response Queue Size: ", followed by a number, this is a line of interest.
    • If that number turns out to not be zero, print the line to your screen.

    The code I provided above follows those three steps: The while loop iterates over the file, the 'if' statement looks for a pattern of interest, and the 'print unless' statement prints the line unless that line of interest has a number equal to zero associated with it.


    Dave