从perl中的文件匹配模式

时间:2013-07-10 08:11:37

标签: linux perl grep

我想在perl中使用grep,但我在这里很困惑。 我想要做的是,有一个像

这样的文件
this is abctemp1
this is temp2
this is temp3x
this is abctemp1

现在,我想使用模式'temp'从这个文件中提取唯一的单词,即'abctemp1,temp2,temp3x',并希望将其存储在数组中。怎么做?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

每一行的字词都在@F中,如果包含@r并且尚未显示,则会被推送到temp

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