如果我理解正确的话,这个程序应该做的你在做什么:
use JSON qw(decode_json encode_json);
use strict;
use warnings;
# set the input line separator to undefined so the next read (<>) reads the entire file
undef $/;
# read the entire input (stdin or a file passed on the command line) and parse it as JSON
my $data = decode_json(<>);
my $from_field = "field6";
my $to_field = "field30";
for (@$data) {
$_->{$to_field} = $_->{$from_field};
}
print encode_json($data), "\n";
它依靠的JSON模块上安装,您可以通过cpanm安装(这应该是最现代的Perl发行版中提供):
cpanm install JSON
如果程序文件substitute.pl
和你的JSON中阵列是data.json
,那么您可以运行它:
perl substitute.pl data.json
# or
cat data.json | perl substitute.pl
应该产生:
[{"field30":"value6","field6":"value6"},{"field30":"value6","field6":"value6"}]
替换field30
的值为field6
's。
这是你正在尝试做什么?
三个字;不不不。 – tripleee
那么,有了适当的JSON库,Perl应该没问题,实际上。 – tripleee
不要手动做。使用像[Perl JSON]这样的模块(http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm#decode_json) – grebneke