-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-grep.pl
More file actions
116 lines (94 loc) · 2.03 KB
/
Copy pathmin-grep.pl
File metadata and controls
116 lines (94 loc) · 2.03 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use strict;
use warnings;
use Getopt::Std;
use File::Find;
my ($regex, %opts, @files);
getopts("nhir", \%opts) or die "Error: unknown option!";
if($opts{h}) {
usage();
exit 0;
}
@ARGV == 2 or die "Error: missing argument option!";
$regex = defined $opts{i}? qr/$ARGV[0]/i : qr/$ARGV[0]/;
if($opts{r}) {
unless(-d $ARGV[1]) {
die "Error: you must specify a directory when using the -r option!";
}
my %f_opts = ('wanted' => \&track_files, 'no_chdir' => 1);
find(\%f_opts, $ARGV[1]);
}
else {
# Input come from stdin.
if($ARGV[1] eq '-') {
search_file(regex => $regex, line_numbers => $opts{n});
exit 0;
}
push @files, $ARGV[1];
}
foreach my $file (@files) {
search_file(regex => $regex, file => $file, line_numbers => $opts{n});
}
# Search for a match.
#
sub search_file {
my %param = (
'line_numbers' => 0,
'file' => undef,
'regex' => qr//,
@_
);
my $file = undef;
if($param{file}) {
unless(open $file, "<$param{file}") {
print STDERR "Error: couldn't open `$param{file}'!";
return;
}
}
else{
$file = \*STDIN;
}
my @fmt_arg = ();
my $fmt_str = '';
if($param{file}) {
push @fmt_arg, $param{file};
$fmt_str = '%s:';
}
if($param{line_numbers}) {
$fmt_str .= '%d:';
}
$fmt_str .= "%s\n";
my $line_no = 1;
while(<$file>) {
chomp;
if(/$param{regex}/) {
if($param{line_numbers}) {
printf $fmt_str, (@fmt_arg, $line_no, $_);
}
else {
printf $fmt_str, (@fmt_arg, $_);
}
}
$line_no += 1;
}
}
# Find all files under a given directory.
#
sub track_files {
unless(-f $File::Find::name) {
return;
}
push @files, $File::Find::name;
}
# Help message.
#
sub usage {
print <<EOF;
Usage:
$0 [OPTION]... <REGEX> <FILE>...
Options:
-i case insensitive
-n print line numbers
-r search for REGEX in all files under the given directory
-h show this help
EOF
}