Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

pipe - How to use `jq` in a shell pipeline?

I can't seem to get jq to behave "normally" in a shell pipeline. For example:

$ curl -s https://api.github.com/users/octocat/repos | jq | cat

results in jq simply printing out its help text*. The same thing happens if I try to redirect jq's output to a file:

$ curl -s https://api.github.com/users/octocat/repos | jq > /tmp/stuff.json

Is jq deliberately bailing out if it determines that it's not being run from a tty? How can I prevent this behavior so that I can use jq in a pipeline?

Edit: it looks like this is no longer an issue in recent versions of jq. I have jq-1.6 now and the examples above work as expected.


* (I realize this example contains a useless use of cat; it's for illustration purposes only)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing jq provides by default, use the identity filter .:

curl -s https://api.github.com/users/octocat/repos | jq '.' | cat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...