Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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
0
Lomag
Asked:
Lomag
  • 4
  • 3
  • 3
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now