#!/usr/bin/perl # An Unicode-aware grep. It searches patterns in UTF-8 encoded files. # Of course, it is far from having all the options of GNU grep... $pattern = $ARGV[0]; if (! $pattern) { die "Usage: $0 pattern file ..." } if (! $ARGV[1]) { binmode STDIN, ":utf8"; while () { if (/$pattern/) { print $_; } } } else { if ($ARGV[2]) { $multiple = 1; } else { $multiple = 0; } shift @ARGV; for $file (@ARGV) { open(FILE, "<:utf8", $file) or die "Cannot open $file: $!"; while () { if (/$pattern/) { if ($multiple) { print $file, ": "; } print $_; } } } }