Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: I wrote a script to grep

by ksr19742001 (Initiate)
on Jul 22, 2014 at 12:10 UTC ( [id://1094623]=note: print w/replies, xml ) Need Help??


in reply to Re: I wrote a script to grep
in thread I wrote a script to grep

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.

Replies are listed 'Best First'.
Re^3: I wrote a script to grep
by mr_mischief (Monsignor) on Jul 22, 2014 at 14:07 UTC

    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

      Hi mr_mischief
      open $my_file, '<', $fn or die "Cannot open $fn: $!\n";
      Is this line working with use strict; and use warnings;?

      I'd assume you wanted to have rather:

      open my $file, '<', $fn or die "Cannot open $fn: $!\n";
      But then, you also have to change the while loop and your close statement.

      I would also recommend that you localize $/ in a block before changing its value (although it may not be necessary in such a short script).

        You're right. I didn't use strict and warnings this time. I made minimal changes to the original code to make my points. The file handle is called $my_file not because I intended my $file but because the original bareword file handle was MYFILE.

        You have a valid point that strictures and warnings should have also been mentioned. That wasn't part of my intent as I scribbled out a response, but maybe it should have been.

Re^3: I wrote a script to grep
by Anonymous Monk on Jul 22, 2014 at 14:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1094623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-23 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found