我正在尝试根据输入从Shell脚本中的文件获取读取值。我该怎么办?
我尝试了其他方式,但无法读取下面文件中的值。
# dates
dates:
start: '10-09-2018'
end: '10-02-2019'
# filters
filters:
- table: 'employee'
column: 'sex'
operation: '=='
value: M
- table: 'employee'
column: 'department'
operation: 'notin'
value: ['operation', 'sales']
- table: 'organisation'
column: 'org_id'
operation: '=='
value: 124
- table: 'organisation'
column: 'org_name'
operation: '=='
value: XYZ LIMITED
我的预期输出, 如果我通过“ 过滤器”和表名称“ 员工”作为输入,则应该以 sex ='M'和部门<>'operation'获得输出。部门<>'销售'
有人可以帮我吗?
答案 0 :(得分:1)
因此,假设您在该示例中修复了缩进使其是有效的YAML,则以下perl脚本将生成您想要的内容:
#!/usr/bin/perl
use warnings;
use strict;
use YAML::XS qw/LoadFile/;
my ($file, $mapping, $table) = @ARGV;
my $yaml = LoadFile $file;
die "No such mapping: $mapping\n" unless exists $yaml->{$mapping};
my %ops = ('==' => '=', 'notin' => '<>');
my $first = 1;
for my $elem (@{$yaml->{$mapping}}) {
if ($elem->{'table'} eq $table) {
my $col = $elem->{'column'};
my $value = $elem->{'value'};
my $op = $elem->{'operation'};
$op = $ops{$op} // $op;
print ' and ' unless $first;
$first = 0;
if (ref $value eq 'ARRAY') {
print join(" and ", map { "$col $op '$_'" } @$value);
} else {
print "$col $op '$value'";
}
}
}
print "\n";
示例:
$ perl boringname.pl example.yaml filters employee
sex = 'M' and department <> 'operation' and department <> 'sales'
需要YAML::XS模块,可通过您喜欢的CPAN客户端或OS软件包管理器安装(Ubuntu称软件包为libyaml-libyaml-perl
。对其他软件包不确定。)。