wilsonfreitas / csvgrep

Easy and funny searches on text delimited files
http://wilsonfreitas.github.io/csvgrep
MIT License
5 stars 1 forks source link

Problem with Pipe Separated Files. #9

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. cat contacts.csv | tr ',' '|' > contacts.psv
2. csvgrep.pl 'I=|' '$1;$2' contacts.psv

What is the expected output? What do you see instead?
I expected to get the same result as csvgrep on a csv file (except with pipe 
output delimiter)

csvgrep.pl '$1;$2' contacts.csv

Name,Email
Wilson,wilson@popstar.com
Lorie Cabucio,lorie@lovestory.com
Jeckill,jeck@monster.com
Nando,nando@nano.com
Rafaela,rafa@popstar.com
Andrea Martins,andy@popstar.com

Instead I see

,N
,W
,L
,J
,N
,R
,A

What version of the product are you using? On what operating system?

csvgrep 1.0.  Red Hat Enterprise Linux Server release 6.3 (Santiago)

Please provide any additional information below.

Original issue reported on code.google.com by mbken...@gmail.com on 11 Jun 2013 at 11:22

GoogleCodeExporter commented 9 years ago
I found a partial solution. In _getopt change code to: 

        $options{'separator'} = exists $options{'S'} ? $options{'S'} : '';
        if ($options{'separator'}) {
                $options{'input-separator'}  = $options{'separator'};
                $options{'output-separator'} = $options{'separator'};
        } else {
                # if (exists $options{'I'}) {
                #       my $separator = '\|' if ($options{'I'} eq '|');
                #       $options{'input-separator'}  = qr/$separator/;
                # } else {
                #       $options{'input-separator'}  = $DEFAULT_SEPARATOR;
                # }
                $options{'input-separator'} = exists $options{'I'} ? $options{'I'} : $DEFAULT_SEPARATOR_TEXT;
                $options{'output-separator'} = exists $options{'O'} ? $options{'O'} : $DEFAULT_SEPARATOR_TEXT;
        }

        $options{'input-separator'} = quotemeta($options{'input-separator'}); # mbk added to quote characters like | for PSV files
        $options{'input-separator'} = qr/$options{'input-separator'}/; # and convert it into a regexp internal representation

Also, change to the following (add \Q) in the initialization: 

my $DEFAULT_SEPARATOR      = qr/\Q$DEFAULT_SEPARATOR_TEXT/;

if for instance you make a $DEFAULT_SEPARATOR_TEXT='|' as I did to make a 
psvgrep.pl

Also, the documentation online for specifying the option to change delimiter is 
not correct.

With the change above I had to do something like

csvgrep.pl -u '-S|' '$1;$2' contacts.psv 

to change to pipe separated values. 
and not -S='|' or anything like that. 

Original comment by mbken...@gmail.com on 11 Jun 2013 at 11:54