regex - 命令行 bash 如何从网页的 curl 输出中提取 json 文本
问题描述
目前,我有一个如下所示的 curl 请求:
curl -k -X GET -H "Accept: application/json" 'https://somesite.com?id=12345' | recode html..ascii
我将此请求通过管道传输到 recode 以对 html ascii 字符进行编码。生成的输出结果如下所示:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4270 100 4270 0 0 6713 0 --:--:-- --:--:-- --:--:-- 0
0
-
-
:
-
-
:
-
-
<html>
-- <head>
:- <title>Test Cars</title>
- </head>
: <body>
2020-05-11 15:03:34,462 INFO test_123 - Ending import of test.
2020-05-11 15:03:34,462 INFO test_123 - Sending message to the UMB.
2020-05-11 15:03:34,989 INFO test_123 - Sending import message:
Message Headers:
JMSExpiration: 0
JMSPriority: 0
JMSMessageID: null
JMSTimestamp: 0
JMSCorrelationID: null
JMSReplyTo: null
JMSRedelivered: false
JMSType: application/json
Message Properties:
id: 12345
type: test
Message Content:
{
"cars" : [ {
"make" : "honda",
"model" : "accord",
"trim" : "ex"
} ],
"status" : "passed",
"log-url" : "https://somesite.com/logurl"
}
2020-05-11 15:03:35,572 INFO test_123 - Message sent.
</pre>
</body>
</html>
我正在尝试解析这部分输出:
{
"cars" : [ {
"make" : "honda",
"model" : "accord",
"trim" : "ex"
} ],
"status" : "passed",
"log-url" : "https://somesite.com/logurl"
}
并通过管道将其输入 jq 以将其转换为 json。
关于如何解析该 json 以便我可以将其通过管道传输到 jq 的任何想法?
解决方案
假设您的输出与您上面给出的一致,请使用:
sed -n '/^{/,/^}/p'
概念证明
$ sed -n '/^{/,/^}/p' ./tojq
{
"cars" : [ {
"make" : "honda",
"model" : "accord",
"trim" : "ex"
} ],
"status" : "passed",
"log-url" : "https://somesite.com/logurl"
}