2015-04-06 44 views
-1

與單詞列表替換單詞的列表我有話一長串如下如何找到和TextMate中或用Perl

word1 
word2 
word3 
word4 
word5 
word6 
word7 

我願做一個查找和這些單詞替換沒有單獨發現一個詞。編號喜歡創建一個查找目標列表,並只運行一次,例如我的目標列表如下。

word2 
word4 
word6 

我怎樣才能做到這一點,並可以在textmate中完成。替代品也很明顯,但我不熟悉perl腳本。

+2

您的預期輸出是什麼? – serenesat 2015-04-06 10:00:17

+0

它會是word2 word4 word6。我只是試圖找到列表中的目標,所以我可以用另一個單詞 – 2015-04-06 10:38:35

+0

'grep -f file2 file1'單獨替換它們? – TLP 2015-04-06 12:54:44

回答

1

我不完全確定你想要什麼,但它聽起來像你想從第二個文件創建一個正則表達式,並將其應用於第一個中的每一行。類似(未經測試):

use autodie; 
open my $fh, '<', $second_file; 
chomp(my @lines = <$fh>); 
close $fh; 

my $joined = join(q{|}, map { quotemeta($_) } @lines); 
my $qr = qr{ $joined }; 

open $fh, '<', $first_file; 
while(<$fh>){ 
    if(/$qr/){ 
    print; 
    } 
} 
close $fh;