1

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.

3
  • 1
    file name, line number and count ...so you want the count per line? It doesn't make much sense to print line numbers and count per file. Commented Nov 6, 2012 at 9:58
  • why don't use just use awk (en.wikipedia.org/wiki/AWK) for this task? Commented Nov 6, 2012 at 9:59
  • No I wanted to do like this and I wanted to do in perl Commented Nov 6, 2012 at 11:31

3 Answers 3

1

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' 
Sign up to request clarification or add additional context in comments.

1 Comment

#!usr/bin/perl -w my @FILES; #my $string="clk"; opendir(DIR,"/home/prodigydell3/verilog") || die "cannot open dir:$!"; @FILES = readdir DIR; closedir(DIR); foreach my $file (@FILES){ open (FH,"/home/prodigydell3/verilog/") or die "$!"; while(<FH>){ my $string = "clk"; if ($string =/clk/i){ print "FH has the pattern clk in $. \n"; } } }
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; } 

2 Comments

Can't locate FILE/Find.pm in @INC (@INC contains: /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 .) at sample2.pl line 3. BEGIN failed--compilation aborted at sample2.pl line 3.
Previously I got the output that mac_top.v has clk in line 45.but now nothing!!!!!!!
1

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 {} +") 

3 Comments

#!usr/bin/perl -w my @FILES; #my $string="clk"; opendir(DIR,"/home/prodigydell3/verilog") || die "cannot open dir:$!"; @FILES = readdir DIR; closedir(DIR); foreach my $file (@FILES){ open (FH,"/home/prodigydell3/verilog/") or die "$!"; while(<FH>){ my $string = "clk"; if ($string =/clk/i){ print "FH has the pattern clk in $. \n"; } } }
I already have the perl script like this, In this I need to print the line number , file name in each file in the directory and the count of the pattern which I am searching. Right now i am able to print only in the folder verilog but there are also other two foldres in verilog which not geting printed. Please help me on this.
I could not get you what you are saying!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.