Re: request for help with COPY syntax - Mailing list pgsql-sql

From Fernando Hevia
Subject Re: request for help with COPY syntax
Date
Msg-id 043e01c8174c$61157a50$8f01010a@iptel.com.ar
Whole thread Raw
In response to Re: request for help with COPY syntax  ("Chuck D." <pgsql-list@nullmx.com>)
Responses Re: request for help with COPY syntax
List pgsql-sql
> On October 25, 2007 10:57:49 am you wrote:
> >
> > If all you just want to do is strip out the ^M, you can run dos2unix on
> > it, assuming that you are running a *nix distro.
> 
> Well, I guess I could strip the ^M but I'm still left with a $ in the
> middle
> of a field which in the same as the line terminator, so COPY thinks it is
> at
> the end of a line when it is really in the middle of the field.  I really
> wish they would have quoted these fields, but I'm at a loss how to import
> these.
> 

As I understand it when a line starts with $ you would like to merge it with
the previous line.

I suppose you have a file like this:

--- test.txt ---
this is 
$field1, and this is 
$field2

I'll create the test file:

$ printf "this is \n\$field1, and this is \n\$field2\n" > test.txt

(I assume ^M have already been replaced so \n are used instead)

A short C program should do it:

/*------ code listing -----*/
#include <stdio.h>
#include <stdlib.h>

#define NL '\n'
#define FILTER '$'

int main(int argc, char *argv[]) {   FILE *fp;   char c;
   if (argc < 2) fp=stdin;   else {       fp=fopen(argv[1], "r");       if (!fp) {           perror(argv[1]);
exit(1);      }   }
 
   c=fgetc(fp);   while(!feof(fp)) {       if(c==NL) {           c=fgetc(fp);           if(feof(fp)) {
putchar(NL);              break;           }       }       if(c!=FILTER) putchar(c);       c=fgetc(fp);   }   exit
(0);
}
/*------------------*/

compile as:
$ gcc -o test test.c

Execute as:
$ test test.txt
this is  field1, and this is field2


Could this be of help?

Regards,
Fernando.



pgsql-sql by date:

Previous
From: Darnell Brawner
Date:
Subject: hidden versioning system
Next
From: "Chuck D."
Date:
Subject: Re: request for help with COPY syntax