简体   繁体   English

使用 sed 提取 json 值

[英]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

To extract all values in proper list form to a file using sed(LINUX).使用 sed(LINUX) 以正确的列表形式将所有值提取到文件中。

sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' >> <your_new_file_to_save>
  • sed 's/regexp/replacement/g' inputFileName > outputFileName. sed 's/regexp/replacement/g' 输入文件名 > 输出文件名。
  • In some versions of sed, the expression must be preceded by -e to indicate that an expression follows.在某些版本的 sed 中,表达式前面必须有 -e 以指示后面有一个表达式。
  • The s stands for substitute, while the g stands for global, which means that all matching occurrences in the line would be replaced. s 代表替代,而 g 代表全局,这意味着该行中的所有匹配项都将被替换。 I've put [ ] inside it as elements that you wanna remove from .json file.我已将 [ ] 作为要从 .json 文件中删除的元素放入其中。
  • The pipe character |管道字符 | is used to connect the output from one command to the input of another.用于将一个命令的输出连接到另一个命令的输入。 then last i did substitute "," and add '\n' known as line breaker.然后最后我确实替换了“,”并添加了称为换行符的“\ n”。

If you want to show a single value see below command:如果要显示单个值,请参见以下命令:

sed 's/["{}\]//g' <your_file.json> | sed 's/,/\n/g' | sed 's/<ur_value>//p'
  • p is run; p 运行; this is equivalent to /pattern match/!这相当于 /pattern match/! p as per above, ie, "if the line does not match /pattern match/ , print it". p 如上所述,即“如果该行不匹配 /pattern match/ ,则打印它”。 So the complete command prints all the lines from the first occurrence of the pattern to the last line, but suppresses the ones that match.所以完整的命令打印从模式的第一次出现到最后一行的所有行,但抑制匹配的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM