在unix中查找特定名称

在unix中查找特定名称,unix,sed,awk,grep,Unix,Sed,Awk,Grep,我想打印满足3个条件的城市:首都、干净和大 输入: 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 NewYo

我想打印满足3个条件的城市:
首都
干净

输入:

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
输出:

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中如何使用
sed
awk

来实现这一点只是为了好玩

壳牌黑客的解决方案:

sort -u input.txt | cut -d' ' -f1 | uniq -dc | egrep '^\s+3\s'
#!/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;
}
perl黑客的解决方案:

sort -u input.txt | cut -d' ' -f1 | uniq -dc | egrep '^\s+3\s'
#!/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;
}
#/usr/bin/perl
严格使用;
使用警告;
使用常数{
资本=>1,
清洁=>2,
大=>4,
};
我的%表;
while()
{
除非m/^(\w+)\s+是一个\s+((大城市)|(清洁城市)|(首都))\s*$/gio,否则打印标准“未解析:$”;
$table{$1}|=已定义($3)*大+
定义(4美元)*清洁+
固定资本(5美元)*;
}
而(我的($k,$v)=每个百分比表)
{
如果(大写+干净+大)=$v,则打印“$k\n”;
}

请向我们展示您尝试过的代码。我尝试过的是。。。在单独的文件中剪切第一列,然后对每个城市执行grep name file | wc-l,只取计数大于3的文件欢迎使用堆栈溢出。请尽快阅读这一页。人们会帮助你解决代码问题;人们不会简单地为您编写代码。请在问题中编辑您迄今为止所尝试的内容。请注意,在第一列运行
cut
会丢弃关于它是哪种城市类型的信息。在您列出的工具中(
grep
sed
cut
awk
),我认为
awk
是解决此问题的首选工具。你需要三个独立的城市列表:干净的、大的和首都。当你阅读了所有的数据后,你可以循环浏览,比如说,所有的大城市,对于每个大城市,检查它是否出现在首都城市和清洁城市列表中,如果出现,打印出来。伦敦是清洁的,堪培拉是大的?天啊!亲爱的,你去哪儿了?