Shashi
Shashi

Reputation: 341

perl REGEX to match mutiple lines

Have issues in setting up a regex for multi-line match

Am trying to grab all failed queries ( receive status is 2 ) without success

Perl one line looks like

perl -e '$text = `cat errors.log`;while($text =~ m/^request.*?(\/.*?)\n.*?receive status is 2$/sgm ){print $1."\n";}'

Sample Log is as follows

***** test 0: *****
* Request:
request = /Lookup?url=URL1

received in 0.108 milliseconds
receive status is 2
-- STATUS : 1 --
      <RESULT SET 1>


***** test 1: *****
* Request:
request = /Lookup?url=URL2

received in 44.753 milliseconds
receive status is 0
-- STATUS : 0 --
      <RESULT SET 2>


***** test 2: *****
* Request:
request = /Lookup?url=URL3

received in 44.753 milliseconds
receive status is 2
-- STATUS : 3 --
      <RESULT SET 3>

Looking at grabbing all the URLs that have failed like URL1,URL3

Upvotes: 1

Views: 187

Answers (2)

TLP
TLP

Reputation: 67910

You should never use

`cat errors.log` 

in Perl. Reading files is something that is simple and efficient in Perl, so learn it instead.

perl -lnwe 'BEGIN { $/= $/x3 } if (/receive status is 2/) 
            { print /request\s*=\s*(.+)/ }'  errors.log

Explanation:

  • -n will treat the arguments to the script as file name arguments and open those files for reading.
  • -l will handle line endings (for print here)
  • The code inside BEGIN sets the input record separator to three newlines (or whatever your OS is using), which allows us to read the input file in chunks, reading up until three consecutive newlines.

And the rest is just two simple regexes. Because we spent some time making sure to keep the chunks separate, we can now use simpler regexes.

Output:

/Lookup?url=URL1
/Lookup?url=URL3

Upvotes: 3

Julian Fondren
Julian Fondren

Reputation: 5609

This is also simple with a bit of state:

perl -lne '$last = $1 if /request = (.+)/; print $last if /^receive status is 2/' errors.log

Storing the entire log in memory becomes less desirable as the log gets larger. And logs have a way of getting to be fairly large.

Upvotes: 0

Related Questions