0

I have a requirement for splitting in perl. Please have a look in the below example:

"john","David2,mick",25,"12-12-2009","male"

I have to split this record on each comma (,) that isn't inside quotes.

Expected output is:

john 
david2mick 
25 
12-12-2009
male

Could you please help me with this.

a sandwhich
  • 4,352
  • 12
  • 41
  • 62
David
  • 11
  • 2
  • 4
    Don't use regexes, use a CSV parser like [Text::CSV](https://metacpan.org/module/Text::CSV). This handles embedded commas correctly. – amon Sep 15 '13 at 17:59
  • 1
    `"David2,mick"` instead of `David2,mick"`? – mpapec Sep 15 '13 at 18:00
  • Your example shows removing quote pairs, removing commas which are inside quote pairs, and then splitting on commas. But that's not exactly what you asked. Please clarify. – dannysauer Sep 15 '13 at 18:02
  • 1
    This is an epic fail. Your sample code and expected output do not match on your most important aspect. – TLP Sep 15 '13 at 18:02
  • 2
    http://stackoverflow.com/a/3065232/223226 – mpapec Sep 15 '13 at 18:04
  • Hi Rohit, Iam not able to install these modules in unix box since I dont have privilages. – David Sep 15 '13 at 18:04
  • This is a CSV file, but I dont have permissions to execute native perl modules... So iam trying with out CSV module – David Sep 15 '13 at 18:35
  • 1
    possible duplicate of [How do I efficiently parse a CSV file in Perl?](http://stackoverflow.com/questions/3065095/how-do-i-efficiently-parse-a-csv-file-in-perl) – JB. Sep 15 '13 at 20:50
  • If you wanted to use a regular expression you could do: ("[^"]*"|[^,]*) – smerlung Sep 16 '13 at 11:14

2 Answers2

6

You can use Text::ParseWords, a core module in Perl 5. It is a light weight option to Text::CSV, assuming this is not proper csv data you have here.

use strict;
use warnings;
use Data::Dumper;
use Text::ParseWords;

my $str = qq("john","David2,mick",25,"12-12-2009","male");
my @list = quotewords(',', 0, $str);

print Dumper \@list;

Output:

$VAR1 = [
          'john',
          'David2,mick',
          '25',
          '12-12-2009',
          'male'
        ];
TLP
  • 66,756
  • 10
  • 92
  • 149
2

This is a CSV file. It's best to use the Text::CSV module

use Text::CSV;
my $line = '"john","David2,mick",25,"12-12-2009","male"';
my $csv = Text::CSV->new();
$csv->parse($line);
my @columns = $csv->fields();
SzG
  • 12,333
  • 4
  • 28
  • 41