From 3210b29bbaa0fcd22c879b6fd830f1bf93ba5159 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Wed, 15 Apr 2015 15:55:27 -0400 Subject: [PATCH] @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 --- builtin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin.c b/builtin.c index 7ef3cfa..1603798 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) -- 2.40.0