Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

extracting single column

by Anonymous Monk
on Feb 09, 2012 at 10:27 UTC ( [id://952682]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello every one, i just have to obtain data from a single column in a file. i am not able to open it in excel how to extract that particular column alone using perl

Replies are listed 'Best First'.
Re: extracting single column
by moritz (Cardinal) on Feb 09, 2012 at 10:32 UTC
Re: extracting single column
by Sewi (Friar) on Feb 09, 2012 at 12:07 UTC
Re: extracting single column
by bcarroll (Pilgrim) on Feb 09, 2012 at 12:28 UTC
    Check out http://search.cpan.org/dist/excel2txt/
    That script will convert each worksheet in an Excel spreadsheet to separate CSV text files. You can then parse each .csv and, split out the column you are looking for.
Re: extracting single column
by gg48gg (Sexton) on Feb 10, 2012 at 00:44 UTC
    #!/bin/perl -w use strict; #for a csv file you can try something like this, #but there are dozens of ways to do this. #you should read a book called "Sam's learn perl in #24 hours" (or similar title). #variables you can change to suit your needs my $column_separator = ","; my $column_number = "3"; $column_number--; my $file="filename.ext"; open(FILE,"<","$file"); my @lines=<FILE>; close FILE; foreach my $line (@lines){ my @columns = split(/$column_separator/,"$line"); print $columns[$column_number],"\n"; }
      There are few lines where I don't agree:

      my $column_separator = ",";
      Better:
      my $column_separator = qr/,/;
      ...imaging someone wants to use "." as separator.

      my $column_number = "3";
      Don't write numbers as text, write them as numbers (cut away those " chars).

      Loading the whole file into memory might not be that good idea, imagine it's a 10 GB file. Why not parse it line-by-line?

      open(FILE,"<","$file"); while (<FILE>) { my @columns = split(/$column_separator/); print $columns[$column_number],"\n"; } close FILE;

      Hey my column has around 900 entries , this code does not extract all of them , but only a part of them . so if you can modify this for me , I will be grateful

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://952682]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found