如何以不同方式pipe理curl答案和打印输出的内容?

我想在命令行中输出curl的一些输出,比如http头,接下来是由stdin / stdout程序处理的答案的正文。 例如:

打印状态码: curl -s -w "%{http_code} \\n" -o "/dev/null" http://myURL.com

然后使用jsonparsing工具处理输出: curl -s http://myURL.com | python -mjson.tool curl -s http://myURL.com | python -mjson.tool

我想用一个命令做这两个,我感觉有可能感谢-o选项,使curl的输出和查询的实际答案之间的区别。 问题是,直接写入文件。 有人有黑客?

你可以使用bash的进程replace>(cmd) 。 例如:

 curl -s -w "%{http_code}\n" -o >(ruby -ne 'printf("-%6s%s", $., $_)') http://www.example.com/ 

另一种可能适合你的需求是curl -v ,它会将标题发送到stderr,并将主体发送到stdout。 例如,

 curl -v http://www.example.com/ | ruby -ne 'printf("-%6s%s", $., $_)'