Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Extract Column of CSV - pretend its a database.

by space_monk (Chaplain)
on Jun 26, 2013 at 08:44 UTC ( #1040729=note: print w/ replies, xml ) Need Help??


in reply to Extract a column of csv

This is an alternative method to the Text::CSV techniques suggested above.

If you're familiar with databases, you can use DBD::CSV to query the CSV file as a database and select the fourth column of data.

If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)


Comment on Re: Extract Column of CSV - pretend its a database.
Re^2: Extract Column of CSV - pretend its a database.
by erix (Vicar) on Jun 26, 2013 at 15:46 UTC

    Why pretend? Assuming these two csv lines:

    11,22,33,"ABC" 1111,3333,4444,"asdfasd asdfasdfasdf"

    And assuming you have the file_fdw extension installed ( create extension file_fdw; ), you could have Pg query it via:

    create server pgcsv foreign data wrapper file_fdw; create foreign table pgcsv ( col1 int, col2 int, col3 int, col4 text ) server pgcsv options ( filename '/tmp/pm-1040691.csv', format 'csv' ); select * from pgcsv; -- or a single column, of course...

    which will output:

    CREATE SERVER CREATE FOREIGN TABLE col1 | col2 | col3 | col4 ------+------+------+---------------------- 11 | 22 | 33 | ABC 1111 | 3333 | 4444 | asdfasd asdfasdfasdf

    Obviously not the easiest way to go about it (and on top of that, postgres is rather picky about the text's precise format). I'm just showing this because you brought up "database" ;)

      Well thats one idea. I presume though you'd need the psql client installed on your PC. Your answer is ingenious but not Perl :-)

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

        You would need much more than psql (which in fact you do not really need); you would need a running postgresql server to talk to. The talking would be done via DBI.

        But again; not really recommended for the OP (or I would have made a DBI example), just a demo for fun :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040729]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-10-31 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (219 votes), past polls