Jq: Add output option to remove line breaks

Created on 16 Nov 2013  路  15Comments  路  Source: stedolan/jq

I'm processing JSON input to extract (and print!) strings that already have trailing \n, and I would like to tell jq to _not_ add a newline character itself after each entry.

feature request

Most helpful comment

Oh, what you want is for jq to not add newlines.

All 15 comments

It's trivial to use tr(1) for this:

jq -c . | tr -d '^J'

(where ^J is generally entered as ctrl-v ctrl-j).

Yes, but your solution doesn't handle more complex cases like strings having \n\n in them. tr will remove those blank lines, but it shouldn't.

With the rtrimstr function (in the features branch of my github clone of jq) you could just trim the trailing newlines from your strings as part of your jq program. I think that is a more general solution.

Yes, but no. If my JSON input contains something like foo\n\n, I want a blank line to appear. With rtrimstr, it won't work.

My l/rtrimstr functions only trim one instance of the string. I guess that means they are misnamed. I should have one set of functions to trim one instance, and another to trim all. Or maybe take an extra argument to indicate how many to trim. Anyways, the functions in my features branch should work for you.

Yes, but no (again). What if there is _no_ trailing \n in the JSON input?
If I understand correctly, using your feature branch, each "message" will end on its own line. Which is not the desired behavior. Right?

Please try it and let me know if it works. If it didn't then please
include a detailed and minimal example.

Can you include an example explaining how to use your rtrimstr function?

I tried this:

./jq -r 'rtrimstr(.log)' <<EOF
{"log": "First line.\n"}
{"log": "Second line.\nThird line.\n"}
{"log": "Fourth "}
{"log": "line.\n"}
{"log": "Fifth line, then a blank.\n\n"}
{"log": "Sixth line, then a blank.\n\nSeventh line.\n"}
EOF

Result:

jq: jv.c:690: jv_string_value: Assertion `jv_get_kind(j) == JV_KIND_STRING' failed.
Aborted

So?

rtimstr trims its argument from its input, so invoke it as

./jq -r 'rtrimstr("\n")' <<EOF
...
EOF

Output:

First line.
Second line.
Third line.
Fourth 
line.
Fifth line, then a blank.

Sixth line, then a blank.

Seventh line.

"Fourth line" should appear on a single line.

This is kind of obvious; what did you expect?

It _does_ appear on a single line with my patch (see attached PR).

Oh, what you want is for jq to not add newlines.

Yup. "Add output option to remove line breaks."
Original description was: "I would like to tell jq to not add a newline character itself after each entry."
Sorry if that wasn't clear.

I got confused, sorry, i don't know why.

I'll include this, but only for raw output.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sonots picture sonots  路  3Comments

neowulf picture neowulf  路  3Comments

kaihendry picture kaihendry  路  4Comments

kelchy picture kelchy  路  4Comments

benjamin-bin-shen picture benjamin-bin-shen  路  3Comments