I also wanted to fetch the first line after the $/ from the beginning. The
line Domain : perl.com should be printed along with the while processing. 

+=========================+
Domain : perl.com


-----Original Message-----
From: Manoj [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, March 25, 2008 1:02 PM
To: 'Chas. Owens'
Cc: 'Perl Beginners'
Subject: RE: Info from flat file

Chas,

I have a question on this. Will the pattern $/ used considered for other
opened files also? I was trying to match the IP with ip-host file and
considering the whole ip-host text file as single line even though there is
newline.

Can you shower some light in this?

Thanks
K

-----Original Message-----
From: Chas. Owens [mailto:[EMAIL PROTECTED] 
Sent: Sunday, March 16, 2008 4:06 AM
To: Manoj
Cc: Perl Beginners
Subject: Re: Info from flat file

On Fri, Mar 14, 2008 at 3:33 PM, Manoj <[EMAIL PROTECTED]> wrote:
> I have a log file like this. The part between += are almost similar from
>  which I need to fetch for IP Address and Connection time only for domain
>  perl.com. The perl.com domain may scattered in log. The main intension of
my
>  work is that this log keeps a record of the users who visits websites. I
>  have to get the doc printed that contains specific domain visits and
time.
>  Thought the below one I will take as a sample data. The IP address can be
>  the 10 or 11th line from Domain line. Was first thinking of getting this
by
>  using head and tail command in unix. This works for me but for windows
box
>  this will be a problem as I don't have cygwin installed which I will not
be
>  able to do. All comments are welcomed. Thanks..!
snip

What you need to do is read in the individual records (delimited by
"+=========================+\n") and search for the required fields
with a regex:

#!/usr/bin/perl

use strict;
use warnings;

$/ = "+=========================+\n";

while (<DATA>) {
        chomp;
        next unless length > 0;
        next unless my ($ip, $time) = /Domain :
perl\.com.*IP:(\S+).*Connection Time:(\S+)/s;
        print "$ip $time\n";
}

__DATA__
+=========================+

Domain : perl.com

hostname

....

....

IP:XXX.XXX.XXX.XXX

Connection Time:XXX secs

....

....

....

....

+=========================+

Domain : domain.com

hostname

....

....

IP:XXX.XXX.XXX.XXX

Connection Time:XXX secs

....

....

+=========================+

Domain : education.com

hostname

IP:XXX.XXX.XXX.XXX

....

....

Connection Time:XXX secs

....

....

+=========================+

Domain : perl.com

hostname

IP:XXX.XXX.XXX.XXX

....

....

Connection Time:XXX secs

....

....

+=========================+





-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to