Reputation: 15
I have looked around, but cannot find a tidy working solution to this. I have been trying to use TEXT:CSV_XS so this is not about merely doing something the hard way with a regex. I may not be able to easily install TEXT::CSV, but I do have the XS version.
I simply need to parse into csv fields, which I will later break down into kv pairs.
use Text::CSV_XS;
use Data::Dumper;
my $csv = Text::CSV_XS->new ({ allow_loose_quotes => 1,
allow_whitespace => 1,
eol => $/ });
my $str3 = '09/11/2016 22:05:00 +0000, search_name="ThreatInjection - Rule", search_now=1473644880.000, search="bunchof|stuff1,bunch%of-stuff2", count=100';
my $status = $csv->parse($str3);
my @details = $csv->fields();
print $csv->error_diag ();
print Dumper(\@details);
Resulting output is:
$VAR1 = [
'09/11/2016 22:05:00 +0000',
'search_name="ThreatInjection - Rule"',
'search_now=1473644880.000',
'search="bunchof|stuff1',
'bunch%of-stuff2"',
'count=100'
];
So, the ask is to get search="bunchof|stuff1,bunch%of-stuff2" to remain in one field. I am sure the answer is simple, but, a bit stumped none the less. Any help appreciated.
Upvotes: 1
Views: 164
Reputation: 69314
You can do with with Text::ParseWords which has been included in the standard Perl distribution since forever.
#!/usr/bin/perl
use strict;
use warnings;
use Text::ParseWords;
use Data::Dumper;
my $str3 = '09/11/2016 22:05:00 +0000, search_name="ThreatInjection - Rule", search_now=1473644880.000, search="bunchof|stuff1,bunch%of-stuff2", count=100';
my @details = parse_line(',\s*', 1, $str3);
print Dumper \@details;
Output:
$VAR1 = [
'09/11/2016 22:05:00 +0000',
'search_name="ThreatInjection - Rule"',
'search_now=1473644880.000',
'search="bunchof|stuff1,bunch%of-stuff2"',
'count=100'
];
Upvotes: 1