1

所以,我有一个keys.txt列出每个键(每行一个)的文件,例如

VIEW_ACCOUNT_NAME_LABEL  
VIEW_ACCOUNT_NAME_DESCR  
VIEW_ACCOUNT_STREET_LABEL  
VIEW_ACCOUNT_CITY_SUBURB_LABEL  
VIEW_ACCOUNT_ZIP_POSTCODE_LABEL  
VIEW_ACCOUNT_COUNTRY_LABEL

还有各种匹配的语言文件,它们为键提供值,就像这样en-GB.view.acccount.ini,每行有一个条目,像这样:

VIEW_ACCOUNT_NAME_LABEL="Name:"
VIEW_ACCOUNT_NAME_DESCR="Name of the account holder."
VIEW_ACCOUNT_STREET_LABEL="Street:"
VIEW_ACCOUNT_CITY_SUBURB_LABEL="City/Suburb:"
VIEW_ACCOUNT_ZIP="Zip Code"
VIEW_ACCOUNT_COUNTRY_LABEL="Country"

nb 有许多密钥和语言文件,实际文件有更多条目——通常每种语言超过 1000 个。

我需要能够找到

  1. 语言文件中缺少哪些键(例如VIEW_ACCOUNT_ZIP_POSTCODE_LABEL
  2. 哪些键在语言文件中但不在密钥文件中(通常是过时的键,例如VIEW_ACCOUNT_ZIP

对于第一个要求,我尝试使用grepinvert -v-match 选项,但结果不是我所期望的:

cppl ~ grep -v --file=keys.txt en-GB.view.acccount.ini
VIEW_ACCOUNT_NAME_LABEL="Name:"
VIEW_ACCOUNT_NAME_DESCR="Name of the account holder."
VIEW_ACCOUNT_STREET_LABEL="Street:"
VIEW_ACCOUNT_CITY_SUBURB_LABEL="City/Suburb:"
VIEW_ACCOUNT_ZIP="Zip Code"
cppl ~ 
4

3 回答 3

4

使用comm.

要查找语言文件中缺少哪些键:

$ comm -23 <(sort keys.txt) <(cut -d= -f1 en-GB.view.acccount.ini | sort) 
VIEW_ACCOUNT_ZIP_POSTCODE_LABEL

要查找哪些密钥在语言文件中但不在密钥文件中:

$ comm -13 <(sort keys.txt) <(cut -d= -f1 en-GB.view.acccount.ini | sort)
VIEW_ACCOUNT_ZIP
于 2013-07-02T07:59:20.927 回答
0

您可以使用标准的 unix 实用程序joinuniq执行此操作。这是一种方法。

我将假设您的密钥文件file1在以下示例中命名。

生成只包含键而不是值的文件。

sed 's/=.*//' en-GB.view.acccount.ini > file2

您现在拥有file1并且file2只包含密钥。对于这个例子:

$ cat file1
A
B
C
D

$ cat file2
C
D
E

您现在可以使用join,sort和的组合uniq来获得所需的输出。

# Keys which are common to both files.
$ join file1 file2 | cat - file1 | sort | uniq -d
C
D

# Keys in file1 but not in file2
$ join file1 file2 | cat - file1 | sort | uniq -u
A
B

# Keys in file2 but not in file1
$ join file1 file2 | cat - file2 | sort | uniq -u
E
于 2013-07-02T07:57:31.317 回答
0

您可以为此使用perl吗?如果是这样,perl 让这变得超级简单。这是我编写的一个快速而肮脏的脚本。修改以适合您的口味。

#!/usr/bin/perl -w

# usage:  validate keys.txt file1.ini [file2.ini [file3.ini [...]]]

open my $keys_file, "<", $ARGV[0] or die "cannot open $ARGV[0] for reading";

my %keys = ( map { chomp; s/\s//g; $_ => 0 } <$keys_file> );

close $keys_file;

sub validate_file
{
    my $filename = shift @_;
    my (@missing, @unexpected, @repeated);
    my %seen = %keys;

    open my $f, "<", $filename or die "cannot open $filename for reading";

    foreach my $line (<$f>)
    {
        chomp $line;

        if ($line =~ /\s*([^=]+)="[^"]*"/)
        {
            if (!defined $seen{$1})
            {
                push @unexpected, $1;
                $seen{$1} = 0;
            }
            $seen{$1}++;
        }
    }

    @missing  = grep { $seen{$_} == 0 } sort keys %keys;
    @repeated = grep { $seen{$_} >  1 } sort keys %keys;

    return \@missing, \@unexpected, \@repeated;
}


shift @ARGV;

foreach my $file (@ARGV)
{
    my ($missing, $unexpected, $repeated) = validate_file($file);

    print "\nFile $file:\n";
    print "Missing keys:\n", join("\n", @$missing), "\n";
    print "Unexpected keys:\n", join("\n", @$unexpected), "\n";
    print "Repeated keys:\n", join("\n", @$repeated), "\n";
}
于 2013-07-02T08:02:36.147 回答