Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flat Text file Split

Status
Not open for further replies.
Jun 16, 2005
52
US
Hello,

I ma currently trying to grab a column from a flat tab delimited text file. I can't seem to get the column I want out of the text file. I would appreciate any help. My perl is horrible. Here is the code I am using:

open (FILE, "$dir$log_file");
while(<FILE>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/\s+//;
$sn =~ s/^\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(FILE);


Here is a sample of the text file. I am trying to grab the Serial Number column:

Current Site Destination Site Serial Number
xxxxx xxxxxx 002345


The code I have works if the serial number column is the second column. I believe I am just missing something small in the code I already have. Thanks for any help you can give me.
 
Hi

($site, $dest_site, $sn) = split;

That should do it.

Oh - skip the first line by adding

$_=<FILE>;

directly after your open() call.

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
If sites are URLs, then your serial number is simply the third space-delimited word on each line, so
Code:
open (FILE, "$dir$log_file");
while(<FILE>){
     chomp;
     @sn = split /\s/;
     push(@list, $sn[2] );
}
close(FILE);
will do the business for you.

Yours,

fish


[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Thanks for the help. Fishiface your code seemed to work, but the output I get (see below the : i have added in thier with other code) gives me all the serial numbers from the Serial Number column. Then at the end of all the serial numbers it writes Serial number. How can I get rid of that.

Code:

open (FILE, "$dir$log_file");
while(<FILE>){
chomp;
@sn = split /\t/;
push(@list, $sn[2] );
}
close(FILE);



output:

005645:005646:005647:005649:005650:005652:005653:005654:005760:Serial Number:
 
Alright so someone give me the dunce hat...After lookin my original code all I had to do was change the [1] to [2]

while(<FILE>){
chomp;
chomp;
$sn = (split(/\t/,$_))[2];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}

-The little baby cow goes:
"MEH MEEEHHHHHHHHHHHH MEHHHHHHHHHHH
 
Code:
open (FILE, "$dir$log_file");
[red]<FILE>; # discard first (column headers) line[/red]
push @list, (split)[2] while <FILE>;
close(FILE);

looks like your best bet. My earlier code ignored the column headers.

You may prefer
Code:
open (FILE, "$dir$log_file");
[red]<FILE>; # discard first (column headers) line[/red]
my @list = map { (split)[2] } <FILE>;
close(FILE);
depending on your hacking heritage.

Yours,

fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top