Skip to content

Instantly share code, notes, and snippets.

@wh13371
Last active August 29, 2015 14:14
Show Gist options
  • Save wh13371/1418c4821ade7eb839b7 to your computer and use it in GitHub Desktop.
Save wh13371/1418c4821ade7eb839b7 to your computer and use it in GitHub Desktop.
perl - "fetch" - a tiny grep/ACK util - more for education than practicality...
#!/usr/bin/perl
# "fetch" - a tiny grep\ACK util - more for education than practicality...
use strict;
use warnings;
use 5.010;
use File::Find;
use Cwd;
use Time::HiRes qw/ time /;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case);
use Term::ANSIColor qw(:constants);
my $pattern;
my ($row_ctr, $total_ctr);
my ($verbose, $debug);
my $ext = '.*';
my $exclude = "exe|mp3|mp4|sqlite|pdf|xls|xlsx|doc|docx|ppt|pptx|iso|gz|tar|jpg|png|gif|jar|zip|7z|m4a|wav|vdi|sav|.*~"; #etc etc
my $dir_exclude = ".git"; # directories to exclude => ".git" by default
my $ignore_case = 0;
my $stdin = 0;
my $yellow = BOLD YELLOW;
my $reset = RESET;
sub parse_args
{
GetOptions("verbose|V", \$verbose,
"debug|v", \$debug,
"ext|e=s", \$ext, # "./fetch.pl error -e pl" => search all ".pl" files
'help|h|?' => \&usage,
"exclude|x=s", \$exclude,
"dexclude|d=s", \$dir_exclude, # ./fetch.pl NULL -Vvi -d "moo|.git" => case insensitive search for "NULL", exclude "moo" and ".git" dirs
"case|i", \$ignore_case,
"stdin|s", \$stdin, # i.e. "find . *.log | ./fetch.pl timeout -s" => get files from <stdin>
"bad|b" => sub # i.e. "./fetch.pl -b" => search for the default values below
{
$pattern = "null|error|timeout|fail|disconnect|exception|warning|off|lost|reset|kill|throw|invalid|down|skip"
} );
}
sub usage
{
print "Usage:\n$0 'tench|roach'\n$0 error\n$0 -b -e .pl\n$0 -b -x pl\n$0 NULL -i\nfind . *.log | $0 timeout -s\n";
exit 58;
}
Getopt::Long::Configure ("bundling"); # -vVi
parse_args();
$pattern = shift || "gudgeon" if !defined($pattern); # ./fetch.pl timeout || if -b use the "bad" string || use "error" if just ./fetch.pl
my @files = ();
if ($stdin)
{
@files = <STDIN>; # i.e. "find . *.log | ./fetch.pl timeout -s"
chomp(@files);
say Dumper @files if $verbose; # -V
}
else
{
my $cwd = getcwd;
# get files\not directorys\exclude specific file types
find( sub { push @files, $File::Find::name if (!-p && -f && $File::Find::dir !~ m/$dir_exclude/ && m/^(.*).$ext$/ && ! m/\.($exclude)$/ ) }, $cwd);
chomp(@files);
}
my $total_start_time = now() if $debug;
say "\nRegex=$pattern" if $verbose; # -V (print the regex pattern)
foreach my $file(@files)
{
open FILE, $file || die $!;
while (my $line = <FILE>)
{
if ($ignore_case)
{
next unless $row_ctr =()= $line =~ /$pattern/gi;
}
else
{
next unless $row_ctr =()= $line =~ /$pattern/g;
}
$ignore_case ? $line =~ s/($pattern)/$yellow$1$reset/gi : $line =~ s/($pattern)/$yellow$1$reset/g;
print BOLD BLUE "\n" . $file, RESET;
print BOLD GREEN "\n[$.]", RESET;
print $line;
$total_ctr += $row_ctr;
say "# Line Matches:$row_ctr" if $debug; # -v
}
close FILE or die $!;
} # foreach <$file> in <@files>
say "\n# Total Matches:$total_ctr" if $debug; # -v
printf "\n%s|%s|%s\n\n", $0, $^T, $^O if $verbose; # -V
say Dumper @files if $verbose; # -V
sub now
{
my ($seconds, $microseconds) = Time::HiRes::gettimeofday;
return $seconds . "." . $microseconds;
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment