bash Grep 匹配某个键/值集 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23938815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep match a certain key/value set json
提问by user3688448
IF THIS IS THE FIRST TIME YOU"RE READING THIS QUESTION, SKIP RIGHT TO THE EDIT
如果这是您第一次阅读此问题,请直接跳到编辑
So what I'm trying to do is match everything until a certain word What I'm working with is similar to this:
所以我想要做的是匹配所有内容,直到某个单词我正在使用的内容与此类似:
{"selling":"0"morestuffhere"notes":"otherthingshere"}unwantedthingshere
The regex I got so far is:
到目前为止我得到的正则表达式是:
grep -o "\{\"selling\":\"0\""
which will match up to {"selling":"0"
.
这将匹配到{"selling":"0"
.
I want it to match {"selling":"0"morestuffhere"notes":"otherthingshere"}
but NOT unwantedstuffhere
.
我希望它匹配{"selling":"0"morestuffhere"notes":"otherthingshere"}
但不匹配unwantedstuffhere
。
I don't know beforehand what "morestuffhere", "otherthingshere" and "unwantedstuffhere" are gonna be. So what I want to do is match everything from what I already have until "notes":"otherthingshere"}
.
我事先不知道“morestuffhere”、“otherthingshere”和“unwantedstuffhere”是什么。所以我想做的是匹配从我已经拥有的到"notes":"otherthingshere"}
.
How do I do this?
我该怎么做呢?
EDIT: forgot to mention some key points. Sorry, had to hurry because dinner was ready.
编辑:忘了提及一些关键点。对不起,因为晚饭准备好了,所以要快点。
My input consists of a series of key:value sets, as such:
我的输入由一系列键:值集组成,如下所示:
{"key":"value", "otherkey":"othervalue","morekeys":"morevalues"},{"othersetkey":"othersetvalue","otherothersetkey":"otherothersetvalue","othersetmorekeys":"othersetmorevalues"}
and so on.
等等。
The first key/value set is different from the rest of them, and I don't want to match that set.
第一个键/值集与其他键/值集不同,我不想匹配该集。
The first key of all sets other than the first is "selling"
, and I want to match all sets that have a "selling" value of 1. The last key of the set is "notes"
.
除第一个之外的所有集合的第一个键是"selling"
,我想匹配所有“销售”值为 1 的集合"notes"
。集合的最后一个键是。
The input is JSON, so I added that to the tags.
输入是 JSON,所以我将它添加到标签中。
回答by Avinash Raj
Through sed,
通过sed,
sed -r 's/^[^{]*([^}]*).*$/}/g' file
Example:
例子:
$ echo 'dSDGAadb{"selling":"0"morestuffhere"notes":"otherthingshere"}unwantedthingshere' | sed -r 's/^[^{]*([^}]*).*$/}/g'
{"selling":"0"morestuffhere"notes":"otherthingshere"}
I think you want something like this,
我想你想要这样的东西,
$ cat aa
dSDGAadb{"selling":"0"morestuffhere"notes":"otherthingshere"}{"selling":"1"morestuffhere"notes":"otherthingshere"}bgj
$ sed -r 's/.*(\{"selling":"1"[^}]*)}.*/}/g' aa
{"selling":"1"morestuffhere"notes":"otherthingshere"}
OR
或者
something like this,
像这样的东西,
$ cat aa
dSDGAadb{"selling":"0"morestuffhere"notes":"otherthingshere"}{"selling":"1"morestuffhere"notes":"otherthingshere"}bgj{"selling":"1"morestuffhere"notes":"otherthingshere"}
$ grep -oP '{\"selling\":\"1\"[^}]*}' aa
{"selling":"1"morestuffhere"notes":"otherthingshere"}
{"selling":"1"morestuffhere"notes":"otherthingshere"}
回答by Tom Fenech
You could do this with grep
:
你可以这样做grep
:
grep -o '{[^}]*}' file
This matches an opening curly brace, followed by anything that isn't a closing curly brace, followed by a closing curly brace.
这匹配一个左花括号,后跟不是右花括号的任何东西,后跟一个右花括号。
Testing it out on your input:
在您的输入上测试它:
$ grep -o '{[^}]*}' <<<'{"selling":"0"morestuffhere"notes":"otherthingshere"}unwantedthingshere'
{"selling":"0"morestuffhere"notes":"otherthingshere"}
回答by Steeve McCauley
I've never found a good way to do this kind of thing with json in the shell with basic unix tools like grep, sed, etc. A quick and dirty ruby or python script is your friend,
我从来没有找到一种在 shell 中使用基本的 unix 工具(如 grep、sed 等)使用 json 执行此类操作的好方法。快速而肮脏的 ruby 或 python 脚本是您的朋友,
#!/usr/bin/env ruby
# h.rb
require 'json'
key=ARGV.shift
json=ARGF.read
h=JSON.parse(json)
puts h.key?(key) ? h[key] : "not found"
And then pipe your json into the script specifying the key as a parameter,
然后将您的 json 管道传输到指定键作为参数的脚本中,
$ echo '{"key":"value", "otherkey":"othervalue","morekeys":"morevalues"}' | /tmp/h.rb otherkey
othervalue
or from a file,
或从文件,
$ cat /tmp/h.json | /tmp/h.rb otherkey
othervalue
回答by timgeb
What's wrong with
怎么了
>> grep -o ".*}" file.txt
{"selling":"0"morestuffhere"notes":"otherthingshere"}
where file.txt
contains your example string?
哪里file.txt
包含您的示例字符串?