-1
#!/bin/bash
filename='delete'

while read p; do 
    jq 'if .tweet | test('\"$p\"'; "i") then . |= . + {vendor: '\"$p\"'} else empty end' sfilter.json
done < $filename

I keep getting this error, when I use variable = "hello world" but not "hello" Inshort whenever there is a space in letter it causes this error.

But while executing directly on shell there seems to be no error.

jq: error: syntax error, unexpected $end, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Unix shell quoting issues?) at <top-level>, line 1:
if .tweet | test("sdas                  
jq: 1 compile error

But when I run the command on shell it works perfectly. Any ideas?

Edit: Input delete file

sdas adssad

Fight Daily
  • 73
  • 1
  • 5

2 Answers2

2

Don't generate a dynamic jq filter using string interpolation. Pass the value via the --arg option.

#!/bin/bash
filename='delete'

while IFS= read -r p; do 
    jq --arg p "$p" 'if .tweet | test($p; "i") then . |= . + {vendor: $p} else empty end' sfilter.json
done < "$filename"
chepner
  • 497,756
  • 71
  • 530
  • 681
1

pass the value of p to your jq program like this:

while read p; do
    jq --arg p "$p" 'if .tweet | test($p; "i") then . |= . + {vendor: $p} else empty end' sfilter.json
done < $filename

The shell error occurs because you concatenated the string incorrectly. Try this:

p=value
JQ_program='if .tweet | test("'"$p"'"; "i") then . |= . + {vendor: "'"$p"'"} else empty end'
echo "$JQ_program"

# result
# if .tweet | test("value"; "i") then . |= . + {vendor: "value"} else empty end
jpseng
  • 1,618
  • 6
  • 18