一些JSON:
{"theSecret":"flhiusdh4543sdfasf34sdf+fsfs/sdf43454=","foo":{"bar":41,"something":"hello world","else":"hi","date":20230101,"qux":0,"digest":"sfhusdf234jkkjuiui23kjkj/SFDF34SDSFDS="}}
字符串
预处理:
{
"theSecret": "flhiusdh4543sdfasf34sdf+fsfs/sdf43454=",
"foo": {
"bar": 41,
"something": "hello world",
"else": "hi",
"date": 20230101,
"qux": 0,
"digest": "sfhusdf234jkkjuiui23kjkj/SFDF34SDSFDS="
}
}
型
我想要theSecret
键的值。
我试过这个:
$ echo "$json" | sed -nE 's/.*theSecret":"(.*)".*/\1/p' # (.*?) doesn't work
型
其给出:
flhiusdh4543................4SDSFDS=
型
即从theSecret
直到digest
的结束。这是因为sed缺少非贪婪量词,所以.*?
不起作用。
我该怎么办?
(This是在一个高山集装箱里,它有sed
,grep
和awk
。jq
和perl
不可用。)
1条答案
按热度按时间mefy6pfw1#
不要使用非贪婪量词,而要使用不匹配终止符
"
的模式。字符串