Skip to content

Instantly share code, notes, and snippets.

@hideo55
Created July 29, 2011 15:42
Show Gist options
  • Save hideo55/1114069 to your computer and use it in GitHub Desktop.
Save hideo55/1114069 to your computer and use it in GitHub Desktop.
keyword coloring on terminal.
#!/usr/bin/env perl -w
use strict;
my $kw_path = shift @ARGV;
if( !defined($kw_path) || !(-e $kw_path ) ){
die <<"__USAGE__";
Usage:
# tail -f | ./color.pl keywords.pl
keywords are defined by ArrayRef such as:
[qw(error fail)];
__USAGE__
}
my $keywords = do $kw_path;
die "Keywords must be ARRAY reference." if !defined $keywords || ( ref($keywords) || q{} ) ne 'ARRAY';
die "You must define keywords one or more" if @$keywords == 0;
my $pattern = join '|', map{ qq{\Q$_\E} } @$keywords;
$pattern = qr/($pattern)/i;
while(<STDIN>){
s/$pattern/\x1b[31m$1\x1b[0m/g;#red
print;
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment