Parse CSV file

M

mmittiga17

Need help folks.

I have a csv file that has some fields containing "," as part of the
field and not a delimiter. These fields are double quoted.

What I need to do is read each line, if the value in field 22 = X dont
print
$line. How can I get the value of each line's field 22, when split
does not work on csv files that have quoted fields?

example: "Potsdam, NY"

This one is driving me crazy.

I have only gotten about this far:

use Text::parseWords;

while (@ARGV) {
$file=shift (@ARGV);
open (IN,"$file") unless ($file =~ /\.Z/);
while (defined($line = <IN>)) {
@fields =quotewords(',', 0, $line);
print "@fields[22]\n";

}
}
I have also tried the regex for csv files from Mastering Regex.

One thought was use a hash...

Any help or suggestions would be appreciated.

Thanks
 
D

David Squire

Need help folks.

I have a csv file that has some fields containing "," as part of the
field and not a delimiter. These fields are double quoted.

What I need to do is read each line, if the value in field 22 = X dont
print
$line. How can I get the value of each line's field 22, when split
does not work on csv files that have quoted fields?

example: "Potsdam, NY"

This one is driving me crazy.

I have only gotten about this far:

use Text::parseWords;

Use the right module for the job. Go to CPAN and search for CSV... or
even "CSV simple" :)


DS
 
X

Xicheng Jia

Need help folks.

I have a csv file that has some fields containing "," as part of the
field and not a delimiter. These fields are double quoted.

What I need to do is read each line, if the value in field 22 = X dont
print
$line. How can I get the value of each line's field 22, when split
does not work on csv files that have quoted fields?

example: "Potsdam, NY"

This one is driving me crazy.

I have only gotten about this far:

use Text::parseWords;

while (@ARGV) {
$file=shift (@ARGV);
open (IN,"$file") unless ($file =~ /\.Z/);
while (defined($line = <IN>)) {
@fields =quotewords(',', 0, $line);

how about(assumed no escaped double-quotes within a field):

@fields = ($line =~ /"[^"]*"|[^",]+/g);
$fields[21] =~ tr/"//d;
print "$fields[21]\n";

Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,732
Members
45,691
Latest member
Dick331194

Latest Threads

Top