postgresql 如何使用 psql -F 设置制表符分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10315916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:28:00  来源:igfitidea点击:

How to set tab delimiter using psql -F

postgresql

提问by Jamie Forrest

I want to dump a select query to a tab-delimited text file using psql -F. However, this doesn't work:

我想使用 psql -F 将选择查询转储到制表符分隔的文本文件。但是,这不起作用:

psql -Umyuser mydb -F '\t' --no-align -c "select * from mytable" -o /tmp/dumpfile.txt

That makes the delimiter a literal \t. How do I get it to use real tabs instead?

这使得分隔符成为文字 \t。我如何让它使用真实的标签?

回答by fbarber

I think you just need to use a literal tab. How this works depends on your shell. Have you seen this post?

我认为您只需要使用文字选项卡即可。这如何工作取决于您的外壳。你看过这个帖子吗?

In the bash shell you can do this with $'\t'.

在 bash shell 中,您可以使用$'\t'.

Using the example in your question:

使用您问题中的示例:

psql -Umyuser mydb -AF $'\t' --no-align -c "select * from mytable" -o /tmp/dumpfile.txt

From man bash:

来自man bash

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. [...] The expanded result is single-quoted, as if the dollar sign had not been present.

$'string' 形式的词被特殊处理。单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。[...] 扩展结果是单引号的,就好像美元符号不存在一样。

回答by Emery Lapinski

In Unix, you can also type

在 Unix 中,您还可以键入

ctrl-V tab

ctrl-V tells the terminal not to interpret the next key.

ctrl-V 告诉终端不要解释下一个键。

This also works with carriage returns (^M) and many other special keys like arrow keys

这也适用于回车 (^M) 和许多其他特殊键,如箭头键

回答by Kamil Bednarz

In case if somebody looked for how to do it in the interactive shell: \f '\t'

如果有人在交互式 shell 中寻找如何执行此操作: \f '\t'