sql - Linux 上的 Postgres 文字回车错误

标签 sql linux postgresql pgadmin

规范:
Linux Mint 18.x
Posgres 10.x
pgadmin3

我尝试将 TSV 加载到 postgres 中,但不断收到回车错误。文件中用“\N”表示NULL。

我尝试过使用 \copy 并使用 pgadmin3 中的导入对话框。在 pgadmin3 中,我尝试省略文件格式,并尝试将其设置为 UTF8 。错误仍然存​​在。

使用的初始命令:

\copy table FROM PROGRAM 'tail -n +2 /home/super/Downloads/folder/myfile.tsv'

ERROR; literal carriage return found in data
HINT: use "\r" to represent carriage return.

我一直在使用 sed 创建不同版本的文件来替换我认为可能导致错误的内容:

sed 's/\n/\r/g myfile.tsv > newfile1.tsv'
sed 's/\\n/\r/g myfile.tsv > newfile2.tsv'
sed 's/\\n//g myfile.tsv > newfile3.tsv'

我还尝试了以下方法(非按时间顺序)

sed 's/\r\n/\r/g' new.tsv

sed 's/\\N/NULL/g' new.tsv

sed 's/\\//' 

sed 's/\\N/\r/' 

sed -n 's/\n/\r/' 

sed -n 's/\\n/\r/' 

sed 's/\N/\r/' 

sed 's/\\N/NULL/' 

sed 's/\N/NULL/' 

sed 's/\r//' 

sed 's/\N/NULL/' 

sed 's/\\N/NULL/' 

sed 's/\\//' 

sed 's/\N/\r/g' new.tsv

sed 's/\N/NULL/g' new.tsv

sed 's/\N/NULL/g' new.tsv  

但这些都不起作用。当我使用 LibreOffice 的预览对话框查看时,它似乎可以滚动内容并将其格式化为表格。

我看过this关于文字换行错误和 this question about using copy 的问题.

我不明白插入错误的“字节”是什么意思。

数据预览:/image/HvOTz.jpg broken tsv

更新:运行sed 's/\r/CR was here/g' myfile.tsv | grep 'CR was here' 返回两个结果

最佳答案

这个问题可以通过使用以下方法解决: sed 's/\r//\\r/g' myfile.tsv > myfile_copy.tsv

显然需要转义两次。

@Abelisto 指出了此评论中出现的错误:

Ok, there is relatively simple way to reproduce the error: echo -e 'aaa\nbbb\rccc' > foo.tsv ; psql -c 'create table foo(x text)' -c '\copy foo from foo.tsv' -c 'drop table foo;' (be careful if you already have valuable foo table in your DB) Lets try to find how to fix it...

也有帮助:

echo -e 'aaa\nbbb\rccc' > foo.tsv ; sed -i 's/\r/\\r/g' foo.tsv ; psql -c 'create table foo(x text)' -c "\copy foo from foo.tsv" -c 'table foo' -c 'drop table foo;' fixed the error. I have no ideas why \r sequence does not translated to CR by copy command as mentioned in the documentation. But it is yet another question. – Abelisto

关于sql - Linux 上的 Postgres 文字回车错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144941/

相关文章:

.net - 更改结果集的排序顺序 'fairly'

c# - 让 Mono 和 Firebird Embedded 在 Linux 上运行

sql - 找不到列 foo 表名 - Teradata

Mysql喜欢反向搜索

php - Mysql:一般错误不正确的整数值

postgresql - 在函数内插入失败,返回 "query has no destination for result data"

sql - 为什么在同一个查询中一个表会被别名两次?

linux - 为什么 OS X 上的 mktemp 会被在 Linux 上运行的命令破坏?

c - 在屏幕上设置装饰 GtkWindow 的框架位置

postgresql - 我如何监听特定模型的创建并基于此创建一个新模型(在不同的表上)?