PostgreSQL 9.5: Skip first two lines in the text file

拟墨画扇 提交于 2021-02-08 05:01:22

问题


I have the text file to import with the following format:

columA    | columnB    |    columnC
-----------------------------------------
1     |     A          |    XYZ
2     |     B          |    XZ
3     |     C          |    YZ

I can skip first line by using:

WITH CSV HEADER; 

in copy command, but got stuck while skipping second line.


回答1:


If you're using COPY FROM 'filename', you could instead use COPY FROM PROGRAM to invoke some shell command which removes the header from the file and returns the rest.

In Windows:

COPY t FROM PROGRAM 'more +2 "C:\Path\To\File.txt"'

In Linux:

COPY t FROM PROGRAM 'tail -n +3 /path/to/file.txt'

If you're trying to send a local file to a remote server, you can do something similar through psql, e.g.:

tail -n +3 file.txt | psql -c 'COPY t FROM STDIN'



回答2:


The COPY command can only skip the first line. The easiest solution would be to manually remove the second line before importing but if that is not possible, then you have to use a "dirty" trick.

You create a table that has a single column of varchar type and import the text file into that table. After import you run a PL/pgSQL function to read all the rows in the table (except the header rows, obviously) and extract the information you want to insert in the destination table with, for instance, the regexp_matches() or regexp_split_to_array() function. You can also automate the whole process by using an after insert trigger on the import table, if you have to import many files with the same issue.



来源:https://stackoverflow.com/questions/42242179/postgresql-9-5-skip-first-two-lines-in-the-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!