[英]Extract json value with sed
I have a json result and I would like to extract a string without double quotes我有一个 json 结果,我想提取一个不带双引号的字符串
{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}
With this regex I can extract the value3 (019-10-24T15:26:00.000Z) correctly使用这个正则表达式,我可以正确提取 value3 (019-10-24T15:26:00.000Z)
sed -e 's/^.*"endTime":"\([^"]*\)".*$/\1/'
How can I extract the "value2" result, a string without double quotes?如何提取“value2”结果,一个没有双引号的字符串?
I need to do with sed so can't install jq.
我需要使用 sed,所以无法安装 jq。 That's my problem
那是我的问题
With GNU sed for -E
to enable EREs:使用 GNU sed for
-E
启用 ERE:
$ sed -E 's/.*"value3":"?([^,"]*)"?.*/\1/' file
2019-10-24T15:26:00.000Z
$ sed -E 's/.*"value2":"?([^,"]*)"?.*/\1/' file
2.5
With any POSIX sed:对于任何 POSIX sed:
$ sed 's/.*"value3":"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/' file
2019-10-24T15:26:00.000Z
$ sed 's/.*"value2":"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/' file
2.5
The above assumes you never have commas inside quoted strings.以上假设您在带引号的字符串中永远没有逗号。
Just run jq
a Command-line JSON processo r只需运行
jq
一个命令行 JSON 处理器
$ json_data='{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}'
$ jq '.value2' <(echo "$json_data")
2.5
with the key .value2
to access the value you are interested in.使用键
.value2
访问您感兴趣的值。
This link summarize why you should NOT use, regex for parsing json (the same goes for XML/HTML and other data structures that are in theory can be infinitely nested)
这个链接总结了为什么你不应该使用正则表达式来解析 json(XML/HTML 和其他理论上可以无限嵌套的数据结构也是如此)
Regex for parsing single key: values out of JSON in Javascript
用于解析单个键的正则表达式:Javascript 中 JSON 中的值
If you do not have jq
available:如果您没有可用的
jq
:
you can use the following GNU grep
command:您可以使用以下 GNU
grep
命令:
$ echo '{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}' | grep -zoP '"value2":\s*\K[^\s,]*(?=\s*,)'
2.5
using the regex detailed here:使用此处详述的正则表达式:
"value2":\s*\K[^\s,]*(?=\s*,)
demo: https://regex101.com/r/82J6Cb/1/演示: https ://regex101.com/r/82J6Cb/1/
This will even work if the json is not linearized!!!!如果 json 没有线性化,这甚至会起作用!!!!
With python
it is also pretty direct and you should have it installed by default on your machine even if it is not python3 it should work使用
python
它也非常直接,你应该在你的机器上默认安装它,即使它不是 python3 它应该可以工作
$ cat data.json
{"value1":5.0,"value2":2.5,"value3":"2019-10-24T15:26:00.000Z","modifier":[]}
$ cat extract_value2.py
import json
with open('data.json') as f:
data = json.load(f)
print(data["value2"])
$ python extract_value2.py
2.5
You can try this :你可以试试这个:
creds=$(eval aws secretsmanager get-secret-value --region us-east-1 --secret-id dpi/dev/hivemetastore --query SecretString --output text )
passwd=$(/bin/echo "${creds}" | /bin/sed -n 's/.*"password":"\(.*\)",/\1/p' | awk -F"\"" '{print $1}')
it is definitely possible to remove the AWK part though ...虽然绝对可以删除 AWK 部分...
如果您的数据在“d”文件中,请尝试 gnu sed
sed -E 's/[{,]"\w+":([^,"]+)/\1\n/g ;s/(.*\n).*".*\n/\1/' d
sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' >> <your_new_file_to_save>
sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' | sed 's/<ur_value>//p'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.