我的 json 文件看起来像这样
[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]
Run Code Online (Sandbox Code Playgroud)
我想为给定的“产品”grep/sed/awk 'id'。
输出:
Enter product : Apple
Your product id is : 2134
Run Code Online (Sandbox Code Playgroud)
使用 JSON 感知工具。Perl 有JSON库:
#!/usr/bin/perl
use warnings;
use strict;
use JSON;
my $json = '[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]';
print 'Enter product: ';
chomp( my $product = <> );
print 'Your product id is: ',
(grep $_->{product} eq 'Apple', @{ from_json($json) })[0]{id}, "\n";
Run Code Online (Sandbox Code Playgroud)