Multiple Spaces Delimited Text File

I have a text file that looks like so:

# 2003/03/18 22:00:00
# Host      In (bytes)    Out (bytes)  Total (bytes)
x.x.x.x     3011555       182187034    212302289
y.y.y.y     67372232      10843221     176015453

Using perl, how can I read this file into 4 variables $host, $inbytes, $outbytes, and $totbytes.
Also I need to ignore the first 2 lines of the file.

I tried doing the usual:
($host, $inbytes, $outbytes, $totbyes) = split(/ /, $line);

But since there's multiple spaces in between each field (usually different amount of spaces), it does not work as I had hoped. How can I get this to work?

I've been searching and have come up clueless. I'm fairly sure this has a simple solution i'm going to kick myself for when I see it.

TIA

Mark
LomagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prady_21Commented:
Try this
while (< STDIN>) {
  ### delete the first two lines here
  $_ =~ /\s+/ /g;
  ($host, $inbytes, $outbytes, $totbyes) = split(/ /);
}
0
LomagAuthor Commented:
the system doesnt like that regexp

Illegal division by zero at test.pl line 7, <FILE> chunk 1.
0
ozoCommented:
<>;
<>;
while( <> ){
    ($host,$inbytes,$outbytes,$totbytes) = split;
}
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LomagAuthor Commented:
ozo,

Your code doesn't seem to make sense to me. I've figured out how to ignore the first two lines which was easy, just had to count each line and do if $linecount <= 2 then ignore them.

The splitting still isn't working for me. Amazing quick replies here!
0
prady_21Commented:
i am sorry abt that
just try this

$_ =~ s/\s+/ /g;

in place of the above one
i am extremely sorry
this one works
i tried it out

#!/usr/bin/perl

### a test program
 $i = 0;
 while ( <STDIN> ) {
   $line = $_;
   if ( $i <= 2 ) { $i++; next; }
   $line =~ s/\s+/ /g;
   ($host, $inbytes, $outbytes, $totbyes) = split(/ /, $line);
   print "$host\t", "$inbytes\t", "$outbytes\t", "$totbyes\n";
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
<>;
<>;
while( <> ){
    ($host,$inbytes,$outbytes,$totbytes) = split;
    print "$host\t$inbytes\t$outbytes\t$totbytes\n";
}
0
LomagAuthor Commented:
Thanks prady. I think that regexp line just eliminats all the extra spaces in each line to a single space so the split can understand it right?

Thanks for the speedy help!!
0
ozoCommented:
you can just split on multiple spaces
   split/ +/;
or just use the default
   split;
0
prady_21Commented:
thanks ozo
i will use that next time
0
LomagAuthor Commented:
Thanks guys, both work well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.