Search code examples
jsonbashcommand-line-interfacejq

How do I use use multiple lines in a jq filter file?


I'm reading the jq manual and it says:

-f filename / --from-file filename:

Read filter from the file rather than from a command line, like awk's -f option. You can also use '#' to make comments.

I typed this into my command line:

$ cat input.json | jq -rf filter.jq

and my filter.jq file is:

.signal[] | .name
# .signal[] | select(.name | contains("signal")) as $matches

This gives me an error:

$ cat input.json | jq -rf filter.jq 
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:

jq: 1 compile error

If I append \ to the end of the first line, I get a different error:

$ cat input.json | jq -rf filter.jq 
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
                  \
jq: 1 compile error

I also tried \\, with similar results.

The only fix I found is to merge the two lines into one. But this limits my ability to document my filter and use the file as a playground for experimentation. How do I use use multiple lines in a jq filter file?


Note: I have tried this on both cygwin and WSL and get the same result.


Solution

  • Your filter.jq contains DOS line endings. Strip them using the dos2unix utility or run your script using the Windows build; jq-win64.exe from the releases page works fine on both Cygwin and WSL2.