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

suppose I have a log file ~ temp.log which contains some information.I need to find 3 patterns from that file..so that if those 3 patterns are found in a file.. then a test case is termed as "passed".
For example : temp.log
20050203 193110776+0900 jpn-docsun1 spamctrl 22551 0 20 Debg;MsgTrace( +1/0) Thread SRMCHandler 1941760 started 20050203 193110777+0900 jpn-docsun1 spamctrl 22551 0 20 Trac;MsgTrace( +1/0) Master:Got a listOp 101 List 4 20050203 193110778+0900 jpn-docsun1 spamctrl 22551 0 15 Trac;MsgTrace( +1/0) Got an update notification 4:101:480344 20050203 193110781+0900 jpn-docsun1 spamctrl 22551 0 12 Debg;MsgTrace( +1/0) spamctrl:begin listssync/expire 20050203 193110782+0900 jpn-docsun1 spamctrl 22551 0 12 Note;BlockItem +Added(1/0) ListAddOp:4:<rbsctest1@openwave.com> 20050203 193110782+0900 jpn-docsun1 spamctrl 22551 0 12 Note;BlockItem +Added(1/0) ListAddOp:4:<rbsctest2@openwave.com> 20050203 193110783+0900 jpn-docsun1 spamctrl 22551 0 12 Note;ListOpDon +e(1/50) LISTADD:List=4:Items=2 20050203 193110783+0900 jpn-docsun1 spamctrl 22551 0 12 Debg;MsgTrace( +1/0) spamctrl:end listssync/expire 20050203 193110792+0900 jpn-docsun1 spamctrl 22551 0 20 Debg;MsgTrace( +1/0) Thread SRMCHandler 1941760 finished 20050203 193111379+0900 jpn-docsun1 spamctrl 22551 0 10 Trac;MsgTrace( +1/0) Received a connection from '10.196.4.110' 20050203 193111380+0900 jpn-docsun1 spamctrl 22551 0 21 Debg;MsgTrace( +1/0) Thread SRMCHandler 1941048 started 20050203 193111391+0900 jpn-docsun1 spamctrl 22551 0 21 Debg;MsgTrace( +1/0) Thread SRMCHandler 1941048 finished 20050203 193111705+0900 jpn-docsun1 spamctrl 22551 0 16 Trac;MsgTrace( +1/0) Handled:<rbsctest1@openwave.com>:[10.196.4.110]:0: 0:: 20050203 193112013+0900 jpn-docsun1 spamctrl 22551 0 12 Debg;MsgTrace( +1/0) spamctrl:begin listssync/expire 20050203 193112014+0900 jpn-docsun1 spamctrl 22551 0 12 Debg;MsgTrace( +1/0) spamctrl:end listssync/expire 20050203 193112728+0900 jpn-docsun1 spamctrl 22551 0 16 Trac;MsgTrace( +1/0) Handled:<rbsctest1@openwave.com>:[10.196.4.110]:0: 0:: 20050203 193112885+0900 jpn-docsun1 spamctrl 22551 0 16 Note;BlockedSe +nder(1/0) <rbsctest1@openwave.com>:[10.196.4.110]:4:3:d ropspam:block


I need to find 2 patterns in this file : 1. Note;ListOpDone(1/50) LISTADD:List=4:Items=2
2. Note;BlockedSender(1/0) <rbsctest1@openwave.com>:[10.196.4.110]:4:3:dropspam:block

I have converted both patterns using quotemeta and they look like this. 1.LISTADD\:List\=4\:Items\=2
2.Note\;BlockedSender\(1\/0\)\ \<rbsctest1\@openwave\.com\>\:\[10\.196\.4\.110\]\:4\:3\:dropspam\:block
How to search for these 2 patterns in an effient way from a file ?

20050207 Janitored by Corion: Put log excerpt into code tags

Replies are listed 'Best First'.
Re: how to search 3 different patterns from a file
by bart (Canon) on Feb 07, 2005 at 08:19 UTC
    I don't know why you subject line says you're searching for 3 patterns, while your question involves just 2.

    Anyway, it would look to me that if you just filter out the lines that contain / Note;/, you're almost home. In fact, you seem to want everything from "Note;" till the end of line. You can try this:

    local $\ = "\n"; while(<>) { if(/ (Note;.*)/) { print $1; } }

    You can put this in a script on its own, pass the path of the log file as a parameter, and see what you get — both in speed, and in filter results.

Re: how to search 3 different patterns from a file
by blazar (Canon) on Feb 07, 2005 at 08:18 UTC
    suppose I have a log file ~ temp.log which contains some information.I need to find 3 patterns from that file..so that if those 3 patterns are found in a file.. then a test case is termed as "passed".
    [SNIP]
    I need to find 2 patterns in this file:
    Were not they 3?!?
    1. Note;ListOpDone(1/50) LISTADD:List=4:Items=2
    2. Note;BlockedSender(1/0) orbsctest1@openwave.com>:[10.196.4.110]:4:3:dropspam:block
    These are not patterns. These are strings. So you want these strings to match, but you fail to explain by which criteria they must match. Or do you want to test for exact equality? (Doubt so!)
    I have converted both patterns using quotemeta and they look like this.
    Well, it's still not entirely clear (to me, that is) what it is that you want to do. But just put the so obtained strings in a match operator and use it. This is basic Perl...
    How to search for these 2 patterns in an effient way from a file ?
    Since you used quotemeta in the first place chances are you do not want a pattern. Just use C<eq> instead.
Re: how to search 3 different patterns from a file
by gube (Parson) on Feb 07, 2005 at 09:10 UTC

    Hi, try this, using one pattern match and try to all pattern like this

    undef $/; open(IN, 'd:\temp.log'); $str = <IN>; (@str) = $str =~ m#(note;.*?)\n#gsi; local $"="\n"; print "@str"; $pass = scalar(@str); if($pass >= 3) { print "Passed"; } else { print "Failed"; }

    Regards,
    Gubendran.L
Re: how to search 3 different patterns from a file
by RazorbladeBidet (Friar) on Feb 07, 2005 at 17:04 UTC
    If you have awk:
    awk '( $8 ~ /Note;BlockedSender\(1\/0\)/ \ $9 ~ /<rbsctest1\@openwave\.com>:\[10\.196\.4\.110]:4:3:dropspa +m:block/) || \ ($8 ~ /Note;ListOpDone\(1\/50\)/)' tmp.log