pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fernando Hevia <fhevia@ip-tel.com.ar>
To: pgsql-list@nullmx.com
To: pgsql-sql@postgresql.org
Subject: Re: request for help with COPY syntax
Date: Thu, 25 Oct 2007 18:16:59 -0300
Message-ID: <043e01c8174c$61157a50$8f01010a@iptel.com.ar> (raw)
In-Reply-To: <200710251313.47075.pgsql-list@nullmx.com>


> 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.




view thread (18+ messages)  latest in thread

Message-ID: <043e01c8174c$61157a50$8f01010a@iptel.com.ar>
Permalink:  ../043e01c8174c$61157a50$8f01010a@iptel.com.ar/
Also on:    postgresql.org/message-id/043e01c8174c$61157a50$8f01010a@iptel.com.ar

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: fhevia@ip-tel.com.ar, pgsql-list@nullmx.com
  Subject: Re: request for help with COPY syntax
  In-Reply-To: <043e01c8174c$61157a50$8f01010a@iptel.com.ar>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox