如何使用perl从目录中逐行读取所有文件?

时间:2012-11-06 09:56:38

标签: perl

例如,我在该文件夹中有一个名为verilog的文件夹,我有更多的文件夹和文件。

我想逐行搜索每个文件中的模式,记录模式匹配的次数,然后打印文件名,行号和计数。

3 个答案:

答案 0 :(得分:1)

文件名和行号:

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

每个文件的文件名和计数:

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

答案 1 :(得分:1)

从“verilog”目录中给出以下命令: 使用grep -R:

对于文件名和行号:

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

对于文件名和计数:

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

答案 2 :(得分: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;
}
相关问题