Re: Importing lines of variable length from ASCII - Mailing list pgsql-novice

From A. Kretschmer
Subject Re: Importing lines of variable length from ASCII
Date
Msg-id 20100225115219.GH16792@a-kretschmer.de
Whole thread Raw
In response to Importing lines of variable length from ASCII  ("Louis Becker" <Louis.Becker@leo.na>)
Responses Re: Importing lines of variable length from ASCII
List pgsql-novice
In response to Louis Becker :
> Hi
>
>
>
> I have an ASCII file that I try to import via the COPY command. The ASCII file
> is pipe delimited. Some lines in the file have no value in the last field and
> because there is no value in the last field, it also omits the last delimiter.
> It therefore seems that some lines have 11 columns while some other lines have
> 10 columns although these 10 columns correspond with the first 10 columns of
> lines that contain 11 columns. The omission of the last delimiter apparently is
> an accordance with the CSV files standard.
>
>
>
> Apart from editing the ASCII files, is there a way to import these files that
> contain lines of variable length?

Pipe your file through a filter like my example below:

kretschmer@tux:~$ cat file.txt
foo|bar|1|batz
bar|bla|2
this|is|3|line
kretschmer@tux:~$ cat file.txt | awk  '{cols=split($0,a,"|"); if (cols==4) print $0; else printf ("%s|\n",$0)}'
foo|bar|1|batz
bar|bla|2|
this|is|3|line
kretschmer@tux:~$


Regards, Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99

pgsql-novice by date:

Previous
From: "Louis Becker"
Date:
Subject: Importing lines of variable length from ASCII
Next
From: Atif Jung
Date:
Subject: Leap Years