-1

我想打印满足 3 个条件的城市capitalcleanbig

输入:

London is a big city
London is a capital
London is a clean city
Ohio is a big city
Sydney is a big city
Sydney is a clean city
Canberra is a capital
Canberra is a big city
Canberra is a clean city
NewYork is a big city
NewYork is a clean city

输出:

London
Canberra

我只需要满足所有三个条件的名称:大写、干净和大。

我尝试cut将第一列放在单独文件中,然后为每个城市做grep name file|wc -l,只取那些计数超过 3 的。如何在 Unix 中使用sedor来完成awk

4

1 回答 1

1

纯娱乐。

shell 黑客的解决方案:

sort -u input.txt | cut -d' ' -f1 | uniq -dc | egrep '^\s+3\s'

perl 黑客的解决方案:

#!/usr/bin/perl
use strict;
use warnings;
use constant {
    CAPITAL => 1,
    CLEAN   => 2,
    BIG     => 4,
};
my %table;

while(<>)
{
    print STDERR "Unparsed: $_" unless m/^(\w+)\s+is a\s+((big city)|(clean city)|(capital))\s*$/gio;

    $table{$1} |= defined($3) * BIG +
                  defined($4) * CLEAN +
                  defined($5) * CAPITAL;
}

while (my ($k,$v) = each %table)
{
    print "$k\n" if (CAPITAL+CLEAN+BIG) == $v;
}
于 2013-07-11T06:38:17.277 回答