views:

61

answers:

3

Hi,

I have a really large SQL dump around 400MB.

It's in the following format, "INSERT INTO user VALUES('USERID', 'USERNAME', 'PASSWORD', '0', '0', 'EMAIL', 'GENDER', 'BIRTHDAY', '182', '13', '640', 'Married', 'Straight', '', 'Yes', 'Yes', '1146411153', '1216452123', '1149440844', '0', picture', '1', '0', '0', 'zip', '0', '', '0', '', '', '0')"

Is there anyway I can just get the email and password out from that, I want to import the users into another table.

Anyone know how I can do this and just get email-password stripped out from that content?

Thank you in advance

+2  A: 

since your question is tagged "perl", one partial solution:

perl -ne '@m = split /,/; print $m[5], $m[2], "\n";' <your400MB.sqldump
PW
There might be commas in the fields though.
Kinopiko
Awesome, works great! Thanks a lot!
zx
with I/O redirection (http://en.wikipedia.org/wiki/Redirection_%28computing%29). BTW Kinopiko is right, that's one reason for which I wrote __partial__ in my answer
PW
A: 

A shorter Perl-oneliner-solution

perl -F, -anE 'say $F[5], $F[2];' file
sid_com
+1  A: 

As Kinopiko said, running split to parse CSV files is not a good idea (e.g. commas inside fields etc...). You should instead use a CSV parser, like this:

use strict;
use Text::CSV_XS; 
my $csv_obj = Text::CSV_XS->new({allow_whitespace=>1, quote_char => "'"})
           || die "Error\n"; 
while (<>) { 
    $csv_obj->parse($_); 
    my @fields = $csv_obj->fields();
    print "$fields[2],$fields[5]\n"
}

NOTE: Not tested since i'm now in an environment has no access to modern CSV_XS :(

DVK
+1 for the proper module to parse CSV, one problem, as you mention, is to have it in the environment ... otherwise, sometimes brute force will do.
PW