使用 Unix 工具提取字符串值
我编写了一个小型 Perl 脚本,用于从给定键名称的 JSON 格式字符串中提取所有值(如下所示)。因此,如果我将 Perl 脚本的命令行开关设置为 id,那么它将从下面的 JSON 示例中返回 1,2 和 stringVal。这个脚本可以完成这项工作,但我想看看其他人如何使用其他 unix 风格的工具(例如 awk、sed 或 perl 本身)解决同样的问题。感谢
{
"id":"1",
"key2":"blah"
},
{
"id":"2",
"key9":"more blah"
},
{
"id":"stringVal",
"anotherKey":"even more blah"
}
提取 JSON 值的 perl 脚本摘录:
my @values;
while(<STDIN>) {
chomp;
s/\s+//g; # Remove spaces
s/"//g; # Remove quotes
push @values, /$opt_s:([\w]+),?/g; # $opt_s is a command line switch for the key to find
}
print join("\n",@values);
I wrote a small Perl script to extract all the values from a JSON formatted string for a given key name (shown below). So, if I set a command line switch for the Perl script to id, then it would return 1,2, and stringVal from the JSON example below. This script does the job, but I want to see how others would solve this same problem using other unix style tools such as awk, sed, or perl itself. Thanks
{
"id":"1",
"key2":"blah"
},
{
"id":"2",
"key9":"more blah"
},
{
"id":"stringVal",
"anotherKey":"even more blah"
}
Excerpt of perl script that extracts JSON values:
my @values;
while(<STDIN>) {
chomp;
s/\s+//g; # Remove spaces
s/"//g; # Remove quotes
push @values, /$opt_s:([\w]+),?/g; # $opt_s is a command line switch for the key to find
}
print join("\n",@values);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当有库可以为您解析字符串时,为什么您要自己解析字符串呢? json.org 拥有 JSON 解析和编码库,几乎适用于您能想到的所有语言(可能还包括一些您没有的语言) t)。在 Perl 中:
.. 生成输出(我在数据周围添加了一个顶级数组,以便将其解析为一个对象):
Why are you parsing the string yourself when there are libraries to do this for you? json.org has JSON parsing and encoding libraries for practically every language you can think of (and probably a few that you haven't). In Perl:
..produces the output (I added a top level array around the data so it would be parsed as one object):
如果您不介意看到引号和冒号字符,我会简单地使用
grep
:grep id file.json
If you don't mind seeing the quote and colon characters, I would simply use
grep
:grep id file.json
使用 JSON;
use JSON;
我强烈建议使用 JSON 模块。它将在一个函数中解析您的 json 输入(并返回)。它还提供了一个 OOP 接口。
I would strongly suggest using the JSON module. It will parse your json input in one function (and back). It also offers an OOP interface.
gawk
输出
如果你只想要 id 值,
gawk
output
If you just want the id value,
下面是一个完成该任务的非常粗略的 Awk 脚本:
-F:
指定 ':' 作为字段分隔符-vk=id
设置您要输入的键寻找。
或“}”被跳过。
摆脱前导空白并且
尾随逗号。
去掉双引号。
匹配 $1,$2 被打印。
data
是包含 JSON 的文件Here is a very rough Awk script to accomplish the task:
-F:
specifies ':' as the field separator-v k=id
sets the key you'researching for.
or '}' are skipped.
gets rid of leading whitespace and
trailing commas.
rid of double quotes.
matches $1, $2 is printed.
data
is the file containing your JSONsed(前提是文件格式如上,每行不超过一个条目):
sed (provided that file is formatted as above, no more than one entry per line):