Lomag
asked on
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
# 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
ASKER
the system doesnt like that regexp
Illegal division by zero at test.pl line 7, <FILE> chunk 1.
Illegal division by zero at test.pl line 7, <FILE> chunk 1.
<>;
<>;
while( <> ){
($host,$inbytes,$outbytes, $totbytes) = split;
}
<>;
while( <> ){
($host,$inbytes,$outbytes,
}
ASKER
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<>;
<>;
while( <> ){
($host,$inbytes,$outbytes, $totbytes) = split;
print "$host\t$inbytes\t$outbyte s\t$totbyt es\n";
}
<>;
while( <> ){
($host,$inbytes,$outbytes,
print "$host\t$inbytes\t$outbyte
}
ASKER
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!!
Thanks for the speedy help!!
you can just split on multiple spaces
split/ +/;
or just use the default
split;
split/ +/;
or just use the default
split;
thanks ozo
i will use that next time
i will use that next time
ASKER
Thanks guys, both work well.
while (< STDIN>) {
### delete the first two lines here
$_ =~ /\s+/ /g;
($host, $inbytes, $outbytes, $totbyes) = split(/ /);
}