CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
[RESOLVED]how to take the previous line

 



danielpcorrea
Novice

Apr 6, 2009, 1:26 AM

Post #1 of 3 (2292 views)
[RESOLVED]how to take the previous line Can't Post

Hi
I'm working with a strange log. It's a mix between timestamp normal log and xml format.

Ex:

20090231175400 | abc | good
20090231175500 | abc | bad
20090231175600 | abc | good
<pb><pms="0123456"></pms></pb>

I need the last timastamp value before the xml row (20090231175600)

My script take the xml line, but I dont know how to take the previous line.


Code
    if ($_ =~ m/pms/) {  
chomp;
$RScont++;
}


Can anybody help me?
thanks
Daniel


(This post was edited by danielpcorrea on Apr 7, 2009, 1:42 AM)


KevinR
Veteran


Apr 6, 2009, 9:52 AM

Post #2 of 3 (2281 views)
Re: [danielpcorrea] how to take the previous line [In reply to] Can't Post

Either cache the previous line in a scalar or use the Tie::File module so you can access the file in the same manner as a perl array. Then the previous line is the index of the current line minus one. See Tie::File for usage. Here is a brief and generic example caching the previous line (untested code):


Code
open(FILE, "thefile") or die "$!"; 
my $pre = <FILE>; #get first line
while(<FILE>){
if (/pms/) {
print $pre;
$RScont++;
}
else {
$pre = $_;
}
}

-------------------------------------------------


danielpcorrea
Novice

Apr 7, 2009, 1:41 AM

Post #3 of 3 (2265 views)
Re: [KevinR] how to take the previous line [In reply to] Can't Post

Thanks.
Works fine.

Daniel

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives