-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfromto
executable file
·87 lines (75 loc) · 1.42 KB
/
fromto
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
#!/usr/bin/perl
# print lines in a file between the <from> pattern and the <to> pattern
# usage: fromto <from> <to> <file>
use strict;
use warnings;
use Getopt::Long;
my $invert;
my $skip;
my $help;
my $result = GetOptions(
"invert|v" => \$invert,
"skip|s" => \$skip,
"help|h" =>);
sub usage
{
print STDERR "Usage: fromto [options] <from pattern> <to pattern> [<file>]\n";
print STDERR "Options:\n";
print STDERR " -s Skip the lines matching the patterns\n";
print STDERR " -v Invert - print the opposite of normal\n";
print STDERR "Notes:\n";
print STDERR " Reads from stdin if no file name is supplied\n";
print STDERR " Patterns are in Perl compatible regex syntax\n";
}
if (!$result)
{
usage;
exit 2;
}
if ($help)
{
usage;
exit 0;
}
my $from = shift @ARGV;
my $to = shift @ARGV;
my $started = 0;
my $skip_boundary = 0;
sub print_boundary {
if ($invert) {
print if $skip_boundary;
} else {
print if !$skip_boundary;
}
}
sub print_middle {
if (!$invert) {
print;
}
}
sub dont_print {
if ($invert) {
print;
}
}
LINE:
while (<>) {
if ($started) {
if ($to && /$to/) {
print_boundary();
$started = 0;
next LINE;
}
else {
print_middle();
next LINE;
}
}
if (!$from || /$from/) {
$started = 1;
print_boundary;
next LINE;
}
dont_print;
next LINE;
}