Skip to content

Instantly share code, notes, and snippets.

@handlename
Created November 26, 2010 17:20
Show Gist options
  • Save handlename/716977 to your computer and use it in GitHub Desktop.
Save handlename/716977 to your computer and use it in GitHub Desktop.
utf8なコマンドラインからjsisのファイルを直接grepしたいときに使うのかも
#!/usr/bin/env perl
use strict;
use warnings;
use Encode;
use File::Find;
use Term::ANSIColor::Print;
my @targets = split /,/, $ARGV[0];
my $dir = $ARGV[1] || '.';
my $print = Term::ANSIColor::Print->new;
find \&wanted, ($dir);
sub is_file {
return -f $_;
}
sub search {
my ($line, $linum) = @_;
Encode::from_to($line, 'sjis', 'utf8');
$line =~ s/\n//;
for my $target (@targets) {
unless ($line =~ /$target/) {
return '';
}
}
return "$linum: $line\n";
}
sub process {
my @path = @_;
my $file = $_;
return unless is_file $file;
my $fh;
my ($line, $linum, $result);
open $fh, '<', $file;
$linum = 0;
while ($line = <$fh>) {
$result .= search $line, ++$linum;
}
if (length $result) {
$print->green(@path);
print $result;
print "\n";
}
close $fh;
}
sub wanted {
process $File::Find::name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment