jq的典型用法
jq [options] <jq filter> [file...]
Run Code Online (Sandbox Code Playgroud)
意味着,例如,要漂亮地打印一个文件,我们会写
jq -s '.' input.json output.json
Run Code Online (Sandbox Code Playgroud)
但随着程序的<filter>增长,将其作为成熟的程序存储在磁盘上会更干净。
我们可能想要这样做,因为:
最好的方法awk(对于 CSV 文件就像 jq 对于 JSON 文件一样)是保存带有.awk扩展名的文件,使其可执行并在第一行写入:
#!/usr/bin/awk -f
Run Code Online (Sandbox Code Playgroud)
或者
#!/opt/local/bin/gawk -f
Run Code Online (Sandbox Code Playgroud)
在磁盘上存储过滤器的好机制是什么jq?
你也可以做同样的事情。从man jq:
\n\nRun Code Online (Sandbox Code Playgroud)\n\xc2\xb7 -f filename / --from-file filename:\n\n Read filter from the file rather than from a command line, like\n awk\xc2\xb4s -f option. You can also use \xc2\xb4#\xc2\xb4 to make comments.\n
例如:
\n$ cat myfilter.jq\n#!/usr/bin/jq --from-file\n\n.[].foo // empty\nRun Code Online (Sandbox Code Playgroud)\n并确保它是可执行的
\n$ chmod +x myfilter.jq\nRun Code Online (Sandbox Code Playgroud)\n然后
\n$ printf '[{"foo": "bar"},{"goo": "bar"},{"foo": "bam"}]' | ./myfilter.jq\n"bar"\n"bam"\nRun Code Online (Sandbox Code Playgroud)\n