Discussion Locked

Perform complex queries on CSV files

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Build query engines in Perl:

The April 29, Perl TechMail discusses complex queries. Have you used Perl to process complex log files and other program output? Will the example in this tip help you parse documents?

If you haven't subscribed to the Perl TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:

http://builder.com.com/techmails.jhtml?repID=u001

3 replies

by felkinmr

Hi,

This just the sort of thing I have been looking for, however, a usage example would help a complete novice like myself.

TIA

by james brown

I’ll have to keep this short due to the posting limit. Sorry there was no space for examples in the original tip.

Assume you have a CSV file (fips.csv) as follows:
Abbr, FIPS Code, State
AL,01,ALABAMA
AK,02,ALASKA
AR,05,ARKANSAS
AZ,04,ARIZONA
CA,06,CALIFORNIA
CO,08,COLORADO
CT,09,CONNECTICUT

The following examples produce the output below them:
perl csvquery.pl "abbr =~ /^A/" < fips.csv
Abbr, FIPS Code, State
AL,01,ALABAMA
AK,02,ALASKA
AR,05,ARKANSAS
AZ,04,ARIZONAperl csvquery_article.pl "abbr =~ /^A/ && fips_code > 2" < fips.csv
Abbr, FIPS Code, State
AR,05,ARKANSAS
AZ,04,ARIZONA

perl csvquery.pl "state =~ /al/i" < fips.csv
Abbr, FIPS Code, State
AL,01,ALABAMA
AK,02,ALASKA
CA,06,CALIFORNIA

Hopethis helps.

- James

This discussion is closed to new replies.