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

Monks,

I have a situation in which I want to search command output for lines containing any of a specified list of strings. I have scoured the net, Perl docs, and written the code below that is still not working. Can someone please help me understand where I am going wrong?

use constant { TRUE => 1, FALSE => 0 }; our $SUCCESS = 0; our %ErrorStrings = ( "No such file or directory" => TRUE, "does not exist in file" => TRUE, ); # Run command. @Output = qx { command here 2>&1 }; $RetCode1=$?; # If the return was success, we need to scan the output for errors. if ($RetCode1 == $SUCCESS) { # Scan the output and build a list of errors. my @FileErrors = grep { exists $ErrorStrings{$_} } @Output; if (@FileErrors) { $RetCode1 = scalar(@FileErrors); printf "Errors found: [%d] ", $RetCode1; push (@ProcErrors, @FileErrors); } # Non success error code. } else { printf "Error Code: [%d] ", $RetCode1; } # Output a status. printf "%-15s\n", ($RetCode1 == 0) ? "SUCCESS":"FAILED";

Which produces:

Processing A_Ra -> A/R ... SUCCESS Processing ADDRESS -> ADDRESS ... SUCCESS Processing ADJ-HIST -> ADJ-HIST ... SUCCESS Processing ALT-PN -> ALT-PN ... SUCCESS Processing AR-LEDGER -> AR-LEDGER ... SUCCESS Processing AR_LEVEL -> AR/LEVEL ... SUCCESS Processing BCBI -> BCBI ... SUCCESS Processing BID -> BID ... SUCCESS Processing BUDGET -> BUDGET ... SUCCESS Processing BUDGET_HEADER -> BUDGET/HEADER ... SUCCESS

And I have intentionally introduced an error into the command being called. What's more, the output of the command with the error is below:

'XORDER' does not exist in file. Open file error.

Please help me understand what I am doing wrong.

Thank you

Replies are listed 'Best First'.
Re: Confusion with finding strings in command output
by Laurent_R (Canon) on Aug 30, 2014 at 17:53 UTC
    This line:
    my @FileErrors = grep { exists $ErrorStrings{$_} } @Output;
    is looking for an exact match of the output line in the hash (i.e. with an hash key), which is probably never going to occur (at the very least, the output line will have a trailing end of line character, but probably also some other words, such as 'XORDER' in your example). You need to use a loop with a regex.
      Thanks. I appreciate the help.
Re: Confusion with finding strings in command output
by Anonymous Monk on Aug 30, 2014 at 18:02 UTC

    If there was a success in opening a file, when why would there be any string in error message about not finding a file (unless for perverse reason the command being called lies about the failure)?

    You are searching for an exact string not for a partial match in grep.

      It is a two-step process: first a shell command run under qx to retrieve the output (including STDERR thanks to the shell redirection), and then an analysis of the output.

        I know that standard error is being redirected. Do look again at OP's code (simplified)...

        my $SUCCESS = 0; my %Error = ( q[No such file or directory] => 1 ); my @out = qx { command here 2>&1 }; my $rc = $?; if ( $rc == $SUCCESS ) { # As I wrote earlier, if command was sucessful, then why would # there be no-file-found error (under normal conditions)? Else, # exit code cannot be trusted. ... = grep { $Error{$_} } @out; ... } ...