Perl script to scan back lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to scan back lines
# 1  
Old 03-18-2008
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.
# 2  
Old 03-18-2008
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?

Code:
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 ...?
era
# 3  
Old 03-18-2008
consider and awk version:
Code:
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)

# 4  
Old 03-18-2008
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 Smilie

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

Last edited by gholdbhurg; 03-18-2008 at 01:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script: matching multiple lines error

Dear Perl users, Could somebody help me how to fix my code so I can get my desired output. Here is the data: Pattern Gabriel halo1 halo2 end Pattern Andreas halo1 halo2 endI want to grep multiple lines between the pattern /Pattern Gabriel / and /end/. Then I will store the output into... (6 Replies)
Discussion started by: askari
6 Replies

2. Shell Programming and Scripting

Scan for anchor tags in Perl?

Hello all, I have some .html files on my hard drive and trying to figure out (if it's possible) how to scan the files in the directory for <a> anchor tags to find linked files. I know how to bring the files in with Perl, but as text. Wondering if there's a way to probe the file for... (2 Replies)
Discussion started by: sldsand
2 Replies

3. Shell Programming and Scripting

Scan a file in realtime and execute certain commands on encountering 5 consecutive identical lines

Mysql log has something like below: I need a bash shell script that will do the following: 1) The script will scan the mysql.log file constantly in real time (something like tail -F mysql.log) 2) If it encounters 5 consecutive identical lines then it would invoke some commands (say... (4 Replies)
Discussion started by: proactiveaditya
4 Replies

4. Shell Programming and Scripting

perl script print the lines between two pattern

i have a file as below sample.pl parameter1 argument1 argument2 parameter2 I want out as below argument1 argument2 that is , i want to print all the lines between parameter1 & parameter 2. i tried with the following if($mystring =~ m/parameter1(.*?)parameter2/) (2 Replies)
Discussion started by: roopa
2 Replies

5. Shell Programming and Scripting

Perl - need script for modify lines

Hello, does somebody can make script for me, which replace: ąćę ąćę ąćę (input file) to ace;|;ąćę ace;|;ąćę ace;|;ąćę (output file) (3 Replies)
Discussion started by: Xadrian
3 Replies

6. Shell Programming and Scripting

Perl script that counts lines of a file

I am working on this script, but hit a bump. Looking for a little help figuring out the last part: open(MY_FILE, $ARGV) or die $COUNTER = 1; $LINE = <FILE>; while ($LINE, <FILE>) { # Adds leading zeros for numbers 1 digit long if ($COUNTER<10){ print "000"; } # Adds... (2 Replies)
Discussion started by: Breakology
2 Replies

7. Shell Programming and Scripting

[Help] PERL Script - grep multiple lines

Hi Gurus, I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element. 1) Generation ID 2) Month 3) Year... (6 Replies)
Discussion started by: miskin
6 Replies

8. UNIX for Dummies Questions & Answers

Perl/shell script count the lines

Hi Guys, I want to write a perl/shell script do parse the following file input file content NPA-NXX SC 2084549 45 2084552 45 2084563 2007 2084572 45 2084580 45 3278411 45 3278430 45 3278493 530 3278507 530... (3 Replies)
Discussion started by: pistachio
3 Replies

9. Shell Programming and Scripting

Perl script to scan through files

Dear perl gurus, I plan to create a script that will scan through a logfile line by line. And if ever a certain line meets the below conditions, it will alert me via email. --> a) Position 10 to 13 = "ABCD" b) And also if the amount specified in position 620-640 is less than the amount in... (1 Reply)
Discussion started by: gholdbhurg
1 Replies

10. UNIX for Dummies Questions & Answers

How to scan only new lines added in file?

Hi, I am planning to implement a scheduled script that will go against my log files (every hour), search for a set of key words (errors, exceptions, faults etc). The script must be intelligent enough to scan only the new lines added to the log file since it last ran. I can use grep for... (3 Replies)
Discussion started by: redlotus72
3 Replies
Login or Register to Ask a Question