문제

I want to remove all double quotes from my csv file, and am struggling to get tr, sed, or perl to work for me and output to file:

perl -e 's/\"//g;" input.csv > output.csv gives no output to screen or file, with output.csv not created

sed 's/\"//g' input.csv > output.csv prints output to the screen, and then gives errors "can't read >: No such file or directory" "can't read output.csv: No such file or directory"

cat input.csv | tr -d '\"' > output.csv gives error "tr: extra operand '>'", then error "write error: No space left on device"

What am I doing wrong?

도움이 되었습니까?

해결책 2

Try this (untested):

tr -d "\"" < input > output

다른 팁

To get your perl version to work, you just need the -p option to indicate printing to the screen:

perl -pe 's/"//g' input.csv > output.csv

Or if you're on windows, do the following:

perl -pe "s/\x22//g" input.csv > output.csv

Also, instead of blindly removing double quotes from a csv, I'd recommend using an actual csv parser to selectively remove double quotes only from those fields where it isn't needed. The following script could do that:

use strict;
use warnings;

use Text::CSV;

my $csv = Text::CSV->new ( { binary => 1, eol => "\n" } )
                or die "Cannot use CSV: ".Text::CSV->error_diag();

if (@ARGV != 1) {
    print "usage: $0 [csvfile]\n";
    exit;
}

while (<>) {
    $csv->parse($_);
    $csv->combine( $csv->fields() );
    print $csv->string();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top