summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2015-04-15 15:55:27 -0400
committerAssaf Gordon <assafgordon@gmail.com>2015-04-15 16:11:58 -0400
commit3210b29bbaa0fcd22c879b6fd830f1bf93ba5159 (patch)
treed02cc68974b22061b9f8f12efd074f6a1ce1c9ec
parentddad9618dca03c93a029a4ec7ee7bcd9e69717fa (diff)
@tsv: escape \r, \n, \\
When using '@tsv' output format with '-r' option, escape \r, \n and \\ in addition to \t. Example: $ printf '{"id":"hello\\ttab\\nworld","x":43 }' | jq . { "id": "hello\ttab\nworld", "x": 43 } Before: newlines are not escaped, generating invalid TSV output: $ printf '{"id":"hello\\ttab\\nworld","x":43 }' \ | ./jq-old -r '[.id,.x] | @tsv' hello\ttab world 43 After: newlines are properly escaped: $ printf '{"id":"hello\\ttab\\nworld","x":43 }' \ | ./jq-new -r '[.id, .x] | @tsv' hello\ttab\nworld 43 Before: backslashes themselves are not escaped, so there's no way to distinguish between escaped characters and 'real' backslashes (in the example below, there is should not be newline, despite the output containing "\n". $ printf '{"x":"hello\\ttab\\\\new world"}' \ | ./jq-old -r '[.x]|@tsv' hello\ttab\new world After: backslashes are escaped: $ printf '{"x":"hello\\ttab\\\\new world"}' \ | ./jq-new -r '[.x]|@tsv' hello\ttab\\new world
-rw-r--r--builtin.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/builtin.c b/builtin.c
index 7ef3cfa4..16037983 100644
--- a/builtin.c
+++ b/builtin.c
@@ -383,7 +383,7 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
assert(!strcmp(fmt_s, "tsv"));
quotes = "";
sep = "\t";
- escapings = "\t\\t\0";
+ escapings = "\t\\t\0\r\\r\0\n\\n\0\\\\\\\0";
}
jv_free(fmt);
if (jv_get_kind(input) != JV_KIND_ARRAY)