Linux perl中文件的模式匹配

Linux perl中文件的模式匹配,linux,perl,grep,Linux,Perl,Grep,我想在perl中使用grep,但我在这里感到困惑。 我想做的是,有一个文件像- this is abctemp1 this is temp2 this is temp3x this is abctemp1 现在,我想使用模式'temp',即'abctemp1,temp2,temp3x'从这个文件中提取唯一的单词,并将其存储在一个数组中。如何做到这一点?严格使用;使用警告; use strict; use warnings; my (@array, %seen); /(\w*temp\w*)/

我想在perl中使用grep,但我在这里感到困惑。 我想做的是,有一个文件像-

this is abctemp1
this is temp2
this is temp3x
this is abctemp1
现在,我想使用模式'temp',即'abctemp1,temp2,temp3x'从这个文件中提取唯一的单词,并将其存储在一个数组中。如何做到这一点?

严格使用;使用警告;
use strict; use warnings;

my (@array, %seen);
/(\w*temp\w*)/ && !$seen{$1}++ && push @array, $1 while <DATA>;
print "$_\n" for @array;

__DATA__
this is abctemp1
this is temp2
this is temp3x
this is abctemp1
我的(@array,%seed); /(\w*temp\w*)/&$看到{$1}++&&push@array,$1 while; 为@array打印“$\un”; __资料__ 这是abctemp1 这是temp2 这是temp3x 这是abctemp1
每行的单词都在
@F
中,如果包含
临时
且尚未看到,则会被推入
@r

perl -anE 'push @r, grep { /temp/ && !$s{$_}++ } @F}{ say for @r' file

非常感谢你。成功了。