Re: request for help with COPY syntax

From: "Fernando Hevia" <fhevia(at)ip-tel(dot)com(dot)ar>
To: <pgsql-list(at)nullmx(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: request for help with COPY syntax
Date: 2007-10-25 21:16:59
Message-ID: 043e01c8174c$61157a50$8f01010a@iptel.com.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Chuck D. 2007-10-26 02:16:15 Re: request for help with COPY syntax
Previous Message Tom Lane 2007-10-25 19:32:15 Re: JOINing based on whether an IP address is contained within a CIDR range?