Perl script to scan back lines

Hi Perl gurus,
I have this file to scan through. Sample lines below:

2008031A, USERNAME, 12345, give ABC, take XYZ, transaction submitted
2008031B, USERNAME, 12346, waiting for processing
2008031C, USERNAME, 12347, Retrieving response
2008031D, USERNAME, 12348, This is not a valid dealing
2008031E, USERNAME, 12349, State has failed
2008031F, USERNAME, 12350, System=0
2008031G, USERNAME, 12351, Waiting for new txns
2008031H, SOMEONE, 12352, give STE, take GVO, transaction submitted
2008031I, SOMEONE, 12353, waiting for processing
2008031J, SOMEONE, 12354, Retrieving response
2008031K, SOMEONE, 12355, This is not a valid dealing
2008031L, SOMEONE, 12356, State has failed
2008031M, SOMEONE, 12357, System=0
2008031N, SOMEONE, 12358, Waiting for new txns

I need to search for this pattern
-->
"This is not a valid dealing"
When one line found a match, it should write in the log the <UserName> as well as the give and take value (i.e. ABC, XYZ)

After scanning above file, error log should appear:
ERROR: USERNAME (ABC, XYZ)
ERROR: SOMEONE (STE, GVO)

Any ideas?

Thanks in advance guys.

Can transactions be intermingled in the log file, or will all details of a transaction be on adjacent lines, and all adjacent lines belong to the same transaction, or mark the boundary to the next transaction?

Does a transaction always start with the "give" and "take" stuff? Does a transaction always end with the "Waiting for new txns" line?

vnix$ perl -ne 'if (/give ([^,]*), take ([^,]*), transaction submitted/) { $give = $1; $take = $2; }
    if (/Waiting for new txns/) { $give = $take = undef; }
    if (/, ([^,]*), [^,]*, This is not a valid/) { print "UPPERCASE: $1 ($give, $take)\n"}' /tmp/txn 
UPPERCASE: USERNAME (ABC, XYZ)
UPPERCASE: SOMEONE (STE, GVO)

I guess all the uppercase is an indication that this is the FINANCIAL SECTOR we are dealing with here ...?

consider and awk version:

csadev:/home/jmcnama> cat t.awk   
awk -F, 'BEGIN { give=""}
         {
            if($4 ~ /give/){
                  give=sprintf("%s%,%s" ,
                        substr($4, length($4)-3),
                        substr($5, length($5)-3 ) )
                  }
            if($4 ~ /This is not a valid dealing/) {
                printf("ERROR: %s (%s)\n", $2, give)
                }
         } ' filename

csadev:/home/jmcnama> cat filename
2008031A, USERNAME, 12345, give ABC, take XYZ, transaction submitted
2008031B, USERNAME, 12346, waiting for processing
2008031C, USERNAME, 12347, Retrieving response
2008031D, USERNAME, 12348, This is not a valid dealing
2008031E, USERNAME, 12349, State has failed
2008031F, USERNAME, 12350, System=0
2008031G, USERNAME, 12351, Waiting for new txns
2008031H, SOMEONE, 12352, give STE, take GVO, transaction submitted
2008031I, SOMEONE, 12353, waiting for processing
2008031J, SOMEONE, 12354, Retrieving response
2008031K, SOMEONE, 12355, This is not a valid dealing
2008031L, SOMEONE, 12356, State has failed
2008031M, SOMEONE, 12357, System=0
2008031N, SOMEONE, 12358, Waiting for new txns

csadev:/home/jmcnama> t.awk
ERROR:  USERNAME ( ABC, XYZ)
ERROR:  SOMEONE ( STE, GVO)

Hi era/jim,
Thanks for the quick response.

>>>
Hi era,
My responses below:

  1. Can transactions be intermingled in the log file, or will all details of a transaction be on adjacent lines, and all adjacent lines belong to the same transaction, or mark the boundary to the next transaction?
    ANS:
    Yes, all details of a transaction will be on adjacent lines, and all adjacent lines belong to the same user/transaction.

  2. Does a transaction always start with the "give" and "take" stuff? Does a transaction always end with the "Waiting for new txns" line?
    ANS:
    Yes that will always be the start line of all txns, but the end lines may vary.
    For successful ones, there wont be any "This is not a valid dealing" line in between.
    >>>

Btw if possible, i would prefer this in perl :slight_smile:

Hope to hear from the other gurus.
Appreciate it very much.