Domanda

I have exported my sql dump in csv format, so suppose my schema was like name,email ,country, I want to remove email column and all its data from csv. what would be the most optimized way to do that either using a tool or any technique.I tried to load that dump in excel but that didn't looked proper Thanks

È stato utile?

Soluzione

you could copy the table inside the mysql database, delete the email column using some mysql client and export back to csv.

Altri suggerimenti

Importing to excel should work with ordered data - you might need to consider alternative delimiters if your data contains commas (such as addresses). If possible use an alternative delimiter, add quote marks around troublesome fields or shift to fixed width output.

Any tool you write or use will need to be able to parse your data and that will always be an issue if the delimiter is scattered through the data.

Alternatively rewrite the view / select / procedure that is generating the data set initially.

This command should do it (assuming a unix* OS):

$ cut -d ',' -f 1,3- dump.csv > newdump.csv

UPDATE: DevZer0 is right, this is unfit for the general case. So you could do (it's tested):

#!/usr/bin/env perl
use Text::ParseWords;
my $file = 'dump.csv';
my $indexOfFieldToBeRemoved = 1; # 0, 1, ...
my @data;
open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n";
while (my $line = <$fh>) {
    chomp $line;
    my @fields = Text::ParseWords::parse_line(',', 0, $line);
    splice(@fields, $indexOfFieldToBeRemoved, 1);
    foreach (@fields) {
        print "\"$_\",";
    }
    print "\n";
}
close $fh;

Sorry, nothing simpler (if you can't re-generate csv dump, as suggested)...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top