在unix中查找特定名称

时间:2013-07-11 05:51:40

标签: unix sed awk grep

我想打印满足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

我只需要满足所有3个条件的名字:资本,清洁和大。

我尝试cut在单独的文件中的第一列然后为每个城市做grep name file|wc -l并且仅使用那些数量超过3的那些。如何在Unix中使用{{1}来完成}或sed

1 个答案:

答案 0 :(得分: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;
}