I am having a problem getting jq to work with the -r
and --arg
options to reference a variable and avoid "shell quoting" problems.
An existing topic here, suggests using --arg
to define a command. I've tried that in this shell function that is designed to take log input and parse it as json if it is parseable as json. If it isn't it just dumps it out as a string.
function pparselogs () {
while read data
do
jq --arg line "$data" -R -r '. as $line | try fromjson catch $line'
done
}
The error that I keep running into is the following:
> echo "Hello World" | pparselogs .
jq: error: syntax error, unexpected '|', expecting '$' or '[' or '{' (Unix shell quoting issues?) at <top-level>, line 1:
. as | try fromjson catch
jq: 1 compile error
Given that the error is complaining about the |
, it seems as though the "$data"
arg value is not being picked up. But given the answer referenced above, this should be the way to do this. I can't see where I'm making a mistake.
Does anyone see the problem here?