Reputation: 35
For example I have a folder called verilog inside that folder I have some more folders and files.
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.
Upvotes: 1
Views: 913
Reputation: 386676
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 {} +")
Upvotes: 1
Reputation: 71
#!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;
}
Upvotes: 1
Reputation: 17054
The below commands to be given from inside the "verilog" directory: Using 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'
Upvotes: 1