简体   繁体   English

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

[英]How to read all files line by line from a directory using perl?

For example I have a folder called verilog inside that folder I have some more folders and files. 例如,我在该文件夹中有一个名为verilog的文件夹,我有一些文件夹和文件。

I want to search for a pattern in each file line by line, keeping count of how many times the pattern has been matched, then print the filename, line number and count. 我想逐行搜索每个文件中的模式,保持模式匹配的次数,然后打印文件名,行号和计数。

File name and line numbers: 文件名和行号:

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

File name and count per file: 每个文件的文件名和计数:

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

The below commands to be given from inside the "verilog" directory: Using grep -R: 以下命令将在“verilog”目录中给出:使用grep -R:

For filename and line numbers: 对于文件名和行号:

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

For filename and count: 对于文件名和计数:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM