[英]How to read all files line by line from a directory using perl?
例如,我在该文件夹中有一个名为verilog的文件夹,我有一些文件夹和文件。
我想逐行搜索每个文件中的模式,保持模式匹配的次数,然后打印文件名,行号和计数。
文件名和行号:
system("find verilog -type f -exec grep -Hn pattern {} +")
每个文件的文件名和计数:
system("find verilog -type f -exec grep -Hc pattern {} +")
以下命令将在“verilog”目录中给出:使用grep -R:
对于文件名和行号:
grep -RHn pattern * | cut -d: -f-2
对于文件名和计数:
grep -RHc india * | awk -F":" '$2!=0'
#!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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.