The following command works in the shell just fine, but when executed via an script it doesn't. What am I missing.
jsonSelectWords='select(.words!=6) | select(.words!=1173) | select(.words!=1) | select(.words!=8) | select(.words!=9) | select(.words!=27)'
cat file.json | jq "$jsonSelectWords"
In the script the select statement is created dynamically, thus I am not able to directly provide it.
input=file.json
local jsonSelectWords="'"
for word in "${wordDupArray[@]}"
do
jsonSelectWords+="select(.words!=$word) | "
done
jsonSelectWords="${jsonSelectWords::-3}"
jsonSelectWords+="'"
cat $input | jq "$jsonSelectWords"
The execution of the last line gives the following error.
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
'select(.words!=6) | select(.words!=1173) | select(.words!=1) | select(.words!=8) | select(.words!=9) | select(.words!=27)'
jq: 1 compile error
Any hints. It tried different variations as well as the whole statement in $(cat $input | jq "$jsonSelectWords")
I have also used the following
cat $input | jq --args JSW "$jsonSelectWords" '$JSW'
(with single quotes removed from the initial string, with '[$JSW]'
and so on). This just outputs the content of jsonSelectWords.
The following lines are examples of the content of file.json
aka $input
.
{"timestamp":"2022-03-09T12:30:23.329630917+01:00","scheme":"http","port":"80","path":"/","body-sha256":"0bfc0bdeb920ce4701f130e6e6a33c8aaf558fae44c7479cc1629930cb0f4535","header-sha256":"d9522b92bb09e71b719804f522f0b3b49aa77974c8d79e644fb45a7b3327f73e","a":["81.91.86.14"],"url":"http://01.akce.omv.com:80","input":"01.akce.omv.com","location":"https://01.akce.omv.com/","webserver":"openresty","content-type":"text/html","method":"GET","host":"81.91.86.14","content-length":95,"status-code":301,"response-time":"194.004475ms","failed":false,"lines":3,"words":6}
{"timestamp":"2022-03-09T12:30:23.355007661+01:00","scheme":"http","port":"80","path":"/","body-sha256":"d6285599bd6f2851fc17e0244ad212a58d8d539231f804f81b5b98289197afa0","header-sha256":"96884ec058c78d0ea282a2d51be4ce0f5c7bc05d8fe3e8dd8f6fb73dd4fa2cd6","a":["81.91.86.14","40.90.4.7","64.4.48.7","2603:1061::7","2620:1ec:8ec::7"],"url":"http://09-mail2.akce.omv.com:80","input":"09-mail2.akce.omv.com","location":"https://09-mail2.akce.omv.com/","webserver":"openresty","content-type":"text/html","method":"GET","host":"81.91.86.14","content-length":101,"status-code":301,"response-time":"233.377898ms","failed":false,"lines":3,"words":6}
{"timestamp":"2022-03-09T12:30:23.450849812+01:00","scheme":"http","port":"80","path":"/","body-sha256":"c186820e328bf631a2943f77e52e9e8319ddfefade6d308a2a22ef996176bbe6","header-sha256":"61e4f3139518b49cac86b77a4f9f06da98d53f2eb12dbff574b5a0ea66327478","a":["81.91.86.14"],"url":"http://09-server2.akce.omv.com:80","input":"09-server2.akce.omv.com","location":"https://09-server2.akce.omv.com/","webserver":"openresty","content-type":"text/html","method":"GET","host":"81.91.86.14","content-length":103,"status-code":301,"response-time":"268.856986ms","failed":false,"lines":3,"words":6}
Solution
local jsonSelectWords=""
for word in "${wordDupArray[@]}"
do
jsonSelectWords+="select(.words!=$word) | "
done
jsonSelectWords="${jsonSelectWords::-3}"
cat $input | jq "$jsonSelectWords"