Обсуждение: postgres copy
Is there away to copy fixed length text files into an Postgresql table. After reading the doc's it looks Postgresql expects some sort of delimiter.
"samsom, debra" wrote: >Is there away to copy fixed length text files into an Postgresql table. >After reading the doc's it looks Postgresql expects some sort of delimiter. Filter the data through perl or awk to insert delimiters. -- Oliver Elphick Oliver.Elphick@lfix.co.uk Isle of Wight http://www.lfix.co.uk/oliver GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C "But they that wait upon the LORD shall renew their strength; they shall mount up with wings as eagles; they shall run, and not be weary; and they shall walk, and not faint." Isaiah 40:31
> "samsom, debra" wrote: > > Is there away to copy fixed length text files into an Postgresql > table. After reading the doc's it looks Postgresql expects some sort > of delimiter. This is how I converted a flat file with fixed field lengths, that was exported from dBASEIV. File has six fields. Third field is a date field. Array flen is for field length and nmchar for type. 0 is for numbers and 1 for text. I have trimmed blanks from the data. If Postgresql data type is varchar, it accepts variable length data. Constant flg is incremented if it keeps reading blank spaces. If flg exceeds 2, program moves ahead without putting out any char. Only change one needs to make in the script are in the initial definition stages. I named this script as bill.c after doing cc bill.c; a.out<bill.txt>tmp gave me the file with "|" as FS. Choice of "|" takes care of text containing a comma. array nmchar is 0 for numeric and 1 for char fields. Vijay #include <stdio.h> #define FNM 6 /* number of fields */ #define F1 6 /* Length of first field */ main() { int c,i,len,count,flg; int flen[] = {6,8,8,7,8,5}; int nmchr[] = {0,0,1,0,0,0}; flg=0; count=0; i=0; len=F1; c=getchar(); while (c != EOF){ if( (nmchr[i]==1)&&(count==0)) printf("'"); if(flg<2) putchar(c); else ; count=count+1; c=getchar(); if (c==' ') flg=flg+1; else flg=0; if ((count==len-1)&&(nmchr[i]==0)&&(c== ' ')) printf("0"); if( (count==len)&&(i<FNM-1)){ if( nmchr[i]==1) printf("'|"); else printf("|"); i=i+1; len=len+flen[i]; if (nmchr[i]==1) printf("'"); } if(c=='\n'){ if (nmchr[i]==1) printf("'"); count=0; i=0; len=F1; putchar(c); c=getchar(); } } return 0; } Vijay