2012-11-06 51 views
1

例如我有一個名爲verilog的文件夾裏面我有更多的文件夾和文件。如何使用perl從目錄中逐行讀取所有文件?

我想逐行搜索每個文件中的模式,保持模式匹配的次數,然後打印文件名,行號和計數。

+1

文件名,行號和計數...所以你要每行計數? 打印行號和每個文件的計數沒有多大意義。 – ikegami

+0

爲什麼不使用awk(http://en.wikipedia.org/wiki/AWK)來執行此任務? – akhilless

+0

不,我想這樣做,我想在perl做 – user1802617

回答

1

文件名和行號:

system("find verilog -type f -exec grep -Hn pattern {} +") 

文件名和每個文件數:

system("find verilog -type f -exec grep -Hc pattern {} +") 
+0

我想要它在perl腳本中 – user1802617

+0

#!usr/bin/perl -w my @FILES; #my $ string =「clk」; opendir(DIR,「/ home/prodigydell3/verilog」)||死「不能打開目錄:$!」; @FILES = readdir DIR; closedir(DIR);打開(FH,「/ home/prodigydell3/verilog /」)或者死掉「$!」;打開我的$文件(@FILES){ while(){ my $ string =「clk」; ($ string =/clk/i) print「FH在$。\ n中有模式clk」; } } } – user1802617

+0

我已經有了像這樣的perl腳本,在這裏我需要在目錄中的每個文件中打印行號,文件名以及我正在搜索的模式的計數。現在我只能在Verilog文件夾中打印,但Verilog中還有其他兩個不打印的文件。請幫助我。 – user1802617

1

下面的命令從 「Verilog的」 目錄內給出: 使用grep -R :

對於文件名和行號:

grep -RHn pattern * | cut -d: -f-2 

文件名和文件數:

grep -RHc india * | awk -F":" '$2!=0' 
+0

#!usr/bin/perl -w my @FILES; #my $ string =「clk」; opendir(DIR,「/ home/prodigydell3/verilog」)||死「不能打開目錄:$!」; @FILES = readdir DIR; closedir(DIR);打開(FH,「/ home/prodigydell3/verilog /」)或者死掉「$!」;打開我的$文件(@FILES){ while(){ my $ string =「clk」; if($ string =/clk/i){ print「FH在$中有模式clk。\ n「;? }} } – user1802617

+0

你能幫助我在此在代碼修改 – user1802617

+0

我一直在grepping爲‘-R’很長一段時間... – slayedbylucifer

1
#!usr/bin/perl 

use strict; 
use File::Find; 
use File::Slurp; 

my $In_dir='some_path'; 
my @all_files; 
my $pattern='test>testing string(\n|\t|\s)</test' 

File::Find:find(
sub{ 
    push @all_files,$File::Find:name if(-f $File::Find:name); 
},$In_dir); 

foreach my $file_(@all_files){ 
    my @file_con=read_file($file_); 
    my $lne_cnt; 
    foreach my $con(@file_con){ 
    my $match_="true" if($con=~m/$pattern/igs); 
    $lne_cnt++; 
    } 
my $tot_line_in_file=$lne_cnt; 
} 
+0

無法找到文件/找到。 pm @inC(@INC包含:/ etc/perl /usr/local/lib/perl/5.12.4 /usr/local/share/perl/5.12.4/usr/lib/perl5/usr/share/perl5/usr/lib/perl/5.12 /usr/share/perl/5.12/usr/local/lib/site_perl。)在sample2.pl第3行。 BEGIN失敗 - 編譯在sample2.pl第3行中止。 – user1802617

+0

以前我得到mac_top.v在第45行有輸出。但現在什麼也沒有!!!!!!! – user1802617