Re: Alter Column Position - Mailing list pgsql-novice

From greg@turnstep.com
Subject Re: Alter Column Position
Date
Msg-id 00d46ff65361db998e07ffe0dc9d5d9f@biglumber.com
Whole thread Raw
In response to Alter Column Position  ("Derrick Betts" <Derrick@grifflink.com>)
List pgsql-novice
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


[...]
> Now, I have to change the position of a column in the DB to match the csv
> parser.  I have a column in position 28 that I need to move to position 7.
>
> Anyone know how to do that?

Why not preprocess the csv file before uploading it to the DB? Here's a way
to do it in quickly in Perl. 10K rows should be no problem:

$,=',';
while(<F>) {
  my @foo = split(/,/ => $_);
  splice(@foo,6,0,splice(@foo,27,1));
  print @foo;
}

or if your csv is not so simple (e.g. contains escaped commas):

use Text::CSV_XS;
$,=',';
my $csv = Text::CSV_XS->new();
while(<F>) {
  die "Cannot parse line $.: " . $csv->error_input . "\n" unless $csv->parse($_);
  my @foo = $csv->fields();
  print @foo[0..5,27,6..26,28..@foo-1]; ## alternative to split()
  print "\n";
}


- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200312102158

-----BEGIN PGP SIGNATURE-----

iD8DBQE/1+GVvJuQZxSWSsgRAp33AKCVXqKTU8qfV585DdQw+ThRWVrF7QCgkC/V
ruF2a1JeWAECM5BGSVbJcSg=
=ZY16
-----END PGP SIGNATURE-----



pgsql-novice by date:

Previous
From: greg@turnstep.com
Date:
Subject: Re: Truncation on restore
Next
From: "Manu M P"
Date:
Subject: createuser problem