![](/img/trans.png)
[英]How to use sed/grep to extract text between two words with condition?
[英]grep Between two words which is output of a curl command
我正在嘗試從以下 curl 輸出中獲取“81”
" {"testing":false,"locked":true,"number":81,"active":true,"service_id":"4bGnyAY1zFM8WBLc49LcbQ","staging":false,"created_at":"2018-11-13T04:18:49Z","deleted_at":null,"comment":"","updated_at":"2018-11-13T04:21:33Z","deployed":false}"
我試過sed -e 's/number\\(.*\\)active/\\1/'
但它反而壓制了這兩個詞......
請幫幫我..我是一個新生
這是 JSON 輸出,不要嘗試用正則表達式解析它。 相反,使用 Python JSON 庫,它是大多數系統的標准配置,也有grep
...
你會想要一個像這里列出的解決方案: https : //www.cambus.net/parsing-json-from-command-line-using-python/
例子:
$ echo '{"testing":false, "locked":true,"number":81,"active":true,"service_id":"4bGnyAY1zFM8WBLc49LcbQ","staging":false,"created_at":"2018-11-13T04:18:49Z","deleted_at":null,"comment":"","updated_at":"2018-11-13T04:21:33Z","deployed":false}' | python -c 'import sys, json; print(json.load(sys.stdin))["number"]'
81
(用你的curl
命令在這里替換 'echo'
或者,如果您可以考慮像jq
這樣的程序,如果它適合您的用例: https : //stedolan.github.io/jq/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.