Monday, December 24, 2012

Perl how to find a exact word from all files under current directory (One level search only)

# Find out where all you see hello word in files
use warnings;
use strict;

my @files = <*.*>;

foreach my $file(@files) {
    if(-e -f $file) {
        open my $file_handler, '<, $file;
        while(<$file_handler>)  {
            if(/\A(hello)\z / ) {
                print $1;
            }
        }
       close $file_handle;
    }
}

No comments: