使用 Unix 工具提取字符串值

发布于 2024-08-11 06:55:25 字数 646 浏览 9 评论 0原文

我编写了一个小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怼怹恏 2024-08-18 06:55:26

当有库可以为您解析字符串时,为什么您要自己解析字符串呢? json.org 拥有 JSON 解析和编码库,几乎适用于您能想到的所有语言(可能还包括一些您没有的语言) t)。在 Perl 中:

use strict;
use warnings;
use JSON qw(from_json to_json);

# enable slurp mode
local $/;

my $string = <DATA>;
my $data = from_json($string);

use Data::Dumper;
print "the data was parsed as: " . Dumper($data);

__DATA__
[
    {
       "id":"1",
       "key2":"blah"
    },
    {
       "id":"2",
       "key9":"more blah"
    },
    {
       "id":"stringVal",
       "anotherKey":"even more blah"
    }
]

.. 生成输出(我在数据周围添加了一个顶级数组,以便将其解析为一个对象):

the data was parsed as: $VAR1 = [
          {
            'key2' => 'blah',
            'id' => '1'
          },
          {
            'key9' => 'more blah',
            'id' => '2'
          },
          {
            'anotherKey' => 'even more blah',
            'id' => 'stringVal'
          }
        ];

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:

use strict;
use warnings;
use JSON qw(from_json to_json);

# enable slurp mode
local $/;

my $string = <DATA>;
my $data = from_json($string);

use Data::Dumper;
print "the data was parsed as: " . Dumper($data);

__DATA__
[
    {
       "id":"1",
       "key2":"blah"
    },
    {
       "id":"2",
       "key9":"more blah"
    },
    {
       "id":"stringVal",
       "anotherKey":"even more blah"
    }
]

..produces the output (I added a top level array around the data so it would be parsed as one object):

the data was parsed as: $VAR1 = [
          {
            'key2' => 'blah',
            'id' => '1'
          },
          {
            'key9' => 'more blah',
            'id' => '2'
          },
          {
            'anotherKey' => 'even more blah',
            'id' => 'stringVal'
          }
        ];
回首观望 2024-08-18 06:55:26

如果您不介意看到引号和冒号字符,我会简单地使用 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

陌若浮生 2024-08-18 06:55:25

我强烈建议使用 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.

不…忘初心 2024-08-18 06:55:25

gawk

gawk 'BEGIN{
 FS=":"
 printf "Enter key name: "
 getline key < "-"
}
$0~key{
  k=$2; getline ; v = $2
  gsub("\"","",k)
  gsub("\"","",v)
  print k,v
}' file

输出

$ ./shell.sh
Enter key name: id
1, blah
2, more blah
stringVal, even more blah

如果你只想要 id 值,

$ key="id"
$ awk -vkey=$key -F":" '$0~key{gsub("\042|,","",$2);print $2}' file
1
2
stringVal

gawk

gawk 'BEGIN{
 FS=":"
 printf "Enter key name: "
 getline key < "-"
}
$0~key{
  k=$2; getline ; v = $2
  gsub("\"","",k)
  gsub("\"","",v)
  print k,v
}' file

output

$ ./shell.sh
Enter key name: id
1, blah
2, more blah
stringVal, even more blah

If you just want the id value,

$ key="id"
$ awk -vkey=$key -F":" '$0~key{gsub("\042|,","",$2);print $2}' file
1
2
stringVal
北斗星光 2024-08-18 06:55:25

下面是一个完成该任务的非常粗略的 Awk 脚本:

awk -v k=id -F: '/{|}/{next}{gsub(/^ +|,$/,"");gsub(/"/,"");if($1==k)print $2}' data
  • -F: 指定 ':' 作为字段分隔符
  • -vk=id 设置您要输入的键
    寻找。
  • 包含 '{' 的行
    或“}”被跳过。
  • 第一个 gsub
    摆脱前导空白并且
    尾随逗号。
  • 第二个 gsub 得到
    去掉双引号。
  • 最后,如果 k
    匹配 $1,$2 被打印。

data 是包含 JSON 的文件

Here is a very rough Awk script to accomplish the task:

awk -v k=id -F: '/{|}/{next}{gsub(/^ +|,$/,"");gsub(/"/,"");if($1==k)print $2}' data
  • the -F: specifies ':' as the field separator
  • The -v k=id sets the key you're
    searching for.
  • lines containing '{'
    or '}' are skipped.
  • the first gsub
    gets rid of leading whitespace and
    trailing commas.
  • The second gsub gets
    rid of double quotes.
  • Finally, if k
    matches $1, $2 is printed.

data is the file containing your JSON

坚持沉默 2024-08-18 06:55:25

sed(前提是文件格式如上,每行不超过一个条目):

KEY=id;cat file|sed -n "s/^[[:space:]]*\"$KEY\":\"//p"|sed 's/".*$//'

sed (provided that file is formatted as above, no more than one entry per line):

KEY=id;cat file|sed -n "s/^[[:space:]]*\"$KEY\":\"//p"|sed 's/".*$//'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文