Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set tab delimiter using psql -F

Tags:

postgresql

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

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?

like image 939
Jamie Forrest Avatar asked Apr 25 '12 12:04

Jamie Forrest


3 Answers

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

Using the example in your question:

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

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

like image 71
fbarber Avatar answered Nov 18 '22 02:11

fbarber


In Unix, you can also type

ctrl-V tab

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

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

like image 30
Emery Lapinski Avatar answered Nov 18 '22 02:11

Emery Lapinski


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

like image 7
Kamil Bednarz Avatar answered Nov 18 '22 00:11

Kamil Bednarz