Parsing tab delimited file with double quotes in Perl
Posted
by
sfactor
on Stack Overflow
See other posts from Stack Overflow
or by sfactor
Published on 2011-01-03T12:30:15Z
Indexed on
2011/01/03
12:53 UTC
Read the original article
Hit count: 406
I have a data set that is tab delimited with the user-agent strings in double quotes. I need to parse each of these columns and based on the answer of my other post I used the Text::CSV module.
94410634 0 GET "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.6; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; AskTB5.5)" 1
The code is a simple one.
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new(sep_char => "\t");
while (<>) {
if ($csv->parse($_)) {
my @columns = $csv->fields();
print "@columns\n";
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
But i get the Failed to parse line:
error when I try it on this dataset. what am I doing wrong? I need to extract the 4th column containing the user-agent strings for further processing.
© Stack Overflow or respective owner