-
Notifications
You must be signed in to change notification settings - Fork 34
/
unigrep.pl
46 lines (34 loc) · 903 Bytes
/
unigrep.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 18 December 2020
# https://github.com/trizen
# A unidecode grep-like program.
# In addition to normal grepping, it also converts input to ASCII and checks the given regex.
# usage:
# perl unigrep.pl [regex] [input]
# find . | perl unigrep.pl [regex]
use 5.010;
use strict;
use warnings;
use Encode qw(decode_utf8);
use Text::Unidecode qw(unidecode);
use Getopt::Std qw(getopts);
my %opt;
getopts('i', \%opt);
my $param = shift(@ARGV) // '';
my $regex = ($opt{i} ? qr/$param/oi : qr/$param/o);
my $uniregex = do {
my $t = decode_utf8($param);
$opt{i} ? qr/$t/io : qr/$t/o;
};
while (<>) {
my $orig = $_;
my $line = decode_utf8($_);
my $unidec = unidecode($line);
if ( $orig =~ $regex
or $line =~ $uniregex
or $unidec =~ $regex
or $unidec =~ $uniregex) {
print $orig;
}
}