如何将通过jq
传输的管道输出写入shell中的文件
例:
curl api.example.com | jq > call.txt
不行。也没有
(curl api.example.com | jq) > call.txt
救命!
编辑:所以做curl api.example.com > call.txt
工作得很好。所以它与管道到jq有关
如果jq
不是终端,只是在没有过滤器的情况下调用stdout
会抛出错误
$ curl https://jsonplaceholder.typicode.com/posts/1 | jq > test.txt
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]
jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
[...]
试试jq '.'
(即:漂亮打印输入JSON):
$ curl https://jsonplaceholder.typicode.com/posts/1 | jq '.' > test.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 292 100 292 0 0 1698 0 --:--:-- --:--:-- --:--:-- 1707
请注意,过滤器不是真正可选的:
来自man jq
:
JQ(1) JQ(1)
NAME
jq - Command-line JSON processor
SYNOPSIS
jq [options...] filter [files...]
根据主分支的提示......你所描述的(以及我观察到的)行为是不可预期的......
旧版本的jq
有以下内容:(here)
if (!program && isatty(STDOUT_FILENO) && !isatty(STDIN_FILENO))
program = ".";
即:如果stdout
是TTY,则使用默认过滤器,stdin
不是TTY。
此行为似乎在commit 5fe05367中得到纠正,使用以下代码片段:
if (!program && (!isatty(STDOUT_FILENO) || !isatty(STDIN_FILENO)))
program = ".";
我的咒语:
$ cat config.json
{
"ProgramSettings":
{
"version": "1.0"
},
"ProgramSecrets":
{
"AWS_ACCESS_KEY_ID": "",
"AWS_SECRET_ACCESS_KEY": ""
}
}
假设您要从JSON文件中删除对象'ProgramSecrets':
$ echo $(cat config.json | jq 'del(.ProgramSecrest)') > config.json
$ cat config.json
{ "ProgramSettings": { "version": "1.0" } }