Search Postgresql Archives

Re: import CSV file to a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Mar 8, 2017 at 3:13 AM, Günce Kaya <guncekaya14@xxxxxxxxx> wrote:
Hi all,

I want to import content of CSV file to a table via bash script without creating temporary table and I also want to skip some columns in CSV file (for instance, CSV file has 12 column and main table has only 2 column, If possible I would use only 2 column in CSV file) Is there any way to do it? 

Regards,

--
Gunce Kaya

​Not too difficult, but "unusual". However, there is a restriction that the data cannot have an embedded comma. That is, you can't have something like: "a,b",c and want two columns with a,b and c as the values.

[tsh009@it-johnmckown-linux junk]$ cat ptest.csv 
a,b,c,d,e,f,g,h,i
1,2,3,4,5,6,7,8,9
z,y,x,w,v,u,t,s,r,q

[tsh009@it-johnmckown-linux junk]$ cat ptest.sh 
#!/bin/bash
printf "%s\n" 'COPY schema1.table1(column1from5, column2from7) FROM stdin;'
export IFS=','
while read i;do # read in the records until EOF
        test -n "${i}" && { # ignore blank lines!
                set ${i} # set shell variables $1, $2, ...
                printf "%s\t%s\n" $5 $7 #write out columns 5 & 7
        }
done
printf "%s\n" '\.' # write EOF delimiter for COPY

[tsh009@it-johnmckown-linux junk]$ ./ptest.sh <ptest.csv 
COPY schema1.table1(column1from5, column2from7) FROM stdin;
e       g
5       7
v       t
\.



--
"Irrigation of the land with seawater desalinated by fusion power is ancient. It's called 'rain'." -- Michael McClary, in alt.fusion

Maranatha! <><
John McKown

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux