?
Solved

Multiple Spaces Delimited Text File

Posted on 2003-03-18
10
Medium Priority
?
244 Views
Last Modified: 2010-05-18
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
Comment
Question by:Lomag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 8164119
Try this
while (< STDIN>) {
  ### delete the first two lines here
  $_ =~ /\s+/ /g;
  ($host, $inbytes, $outbytes, $totbyes) = split(/ /);
}
0
 

Author Comment

by:Lomag
ID: 8164163
the system doesnt like that regexp

Illegal division by zero at test.pl line 7, <FILE> chunk 1.
0
 
LVL 84

Expert Comment

by:ozo
ID: 8164171
<>;
<>;
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!

 

Author Comment

by:Lomag
ID: 8164198
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
 
LVL 3

Accepted Solution

by:
prady_21 earned 100 total points
ID: 8164207
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
 
LVL 84

Expert Comment

by:ozo
ID: 8164228
<>;
<>;
while( <> ){
    ($host,$inbytes,$outbytes,$totbytes) = split;
    print "$host\t$inbytes\t$outbytes\t$totbytes\n";
}
0
 

Author Comment

by:Lomag
ID: 8164243
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
 
LVL 84

Expert Comment

by:ozo
ID: 8164300
you can just split on multiple spaces
   split/ +/;
or just use the default
   split;
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8164982
thanks ozo
i will use that next time
0
 

Author Comment

by:Lomag
ID: 8166880
Thanks guys, both work well.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question