Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Loop through all directory and files and lines

by zentara (Archbishop)
on Oct 18, 2018 at 13:43 UTC ( #1224235=note: print w/replies, xml ) Need Help??

in reply to Loop through all directory and files and lines

Hi, here is a script I use often to search thru my files, it's not the best code, but it works. Notice the commented out lines which skip binary files. Usually, you don't want to search a binary file.
#!/usr/bin/perl use warnings; use strict; use File::Find; $|++; # defaults are case-insensitive, no recurse, open and search files (no +t filename) if ($#ARGV < 0){die "Usage zgrep 'pattern' c(case sensitive optional) +r(recurse optional) n(search name only optional) examples: zgrep 'debug me' r will recursively search all files for 'debug me'\n"; } my ($recurse, $name, $case) =(0,0,0); if( grep{/\bn\b/} @ARGV ){@ARGV = grep { $_ ne 'n' } @ARGV; $name = 1 +}; if( grep{/\br\b/} @ARGV ){@ARGV = grep { $_ ne 'r' } @ARGV; $recurse = + 1 }; if( grep{/\bc\b/} @ARGV ){@ARGV = grep { $_ ne 'c' } @ARGV; $case = 1 +}; #print "$name $recurse $case @ARGV\n"; my $path = '.'; #only accept 1 search string, so quote phrases my $search = $ARGV[0]; my $regex; #defaults to case insensitive if ($case){$regex = qr/\Q$search\E/} else{$regex = qr/\Q$search\E/i} # use s modifier for multiline match find (sub { #skip directories which begin with 1 if (-d && $_ =~ /^1.*$/) { $File::Find::prune = 1; return; } if( ! $recurse ){ my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if ($n > 1); } return if -d; # return unless (-f and -T ); # don't waste time on binaries if($name){ if ($_ =~ /$regex/){print "$File::Find::name\n"}; }else{ return unless (-f and -T ); # don't waste time on binaries open (FH,"< $_"); while(<FH>){ print "$File::Find::name: $. :$_\n " if /$regex/; } close FH; } }, $path); exit;

I'm not really a human, but I play one on earth. ..... an animated JAPH

Replies are listed 'Best First'.
Re^2: Loop through all directory and files and lines
by MissPerl (Sexton) on Oct 18, 2018 at 14:58 UTC
    thank you for the help !

    I'll have a look ! that's very nice.

    However, my priority now is to understand what is wrong with my code, which I still cant figure out till now.. :(
      what is wrong with my code

      My guess is you need to refresh @store_array within the @first_directory loop

      foreach my $first (@first_directory){ my @store_array = qw (1 2 3 4 5 6); # here while(my ($i,$j) = splice(@store_array,0,2)){ my $second_directory = "$first/$i/$j"; ..


      #!/usr/bin/perl use strict; use warnings; use File::Glob 'bsd_glob'; my $store_location = '/path/to/file/store.txt'; open my $fh_log, '>', $store_location or die "Fail to open file '$store_location' $!"; my $Dir1 = "/somewhere"; my $Dir2 = "/somewhereelse"; my @first_directory = ( $Dir1, $Dir2 ); foreach my $first (@first_directory){ my @store_array = qw (1 2 3 4 5 6 7 8); while (my ($i,$j) = splice(@store_array,0,2)){ my $second_directory = "$first/$i/$j"; if (-e $second_directory and -d $second_directory){ my @files = bsd_glob("$first/$i/$j/*"); foreach my $file (@files){ my $lines = 0; my $match = 0; open(FILE, "<" , $file) or die "Can't open file '$file': $!"; + while (<FILE>){ if (/TbhODK|octuov|qas_uop/) { ++$match; } ++$lines; } print "$file : $lines lines read. $match matches found\n"; close FILE; print $fh_log "File $file has $match matches\n"; } } else { print $fh_log "Directory $first/$i/$j (fail to exist)\n"; } } } close $fh_log;

      The problem is your code contains no subroutines , yet contains four loops -- it should contain three subroutines minimum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1224235]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2020-07-11 02:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found