-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutv
executable file
·62 lines (51 loc) · 1.08 KB
/
cutv
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
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
our($opt_l, $opt_d, $opt_f);
$opt_d='^\s*$';
getopt('ldf');
$opt_d = eval { qr/$opt_d/ };
die "invalid regexp for -d: $@" if $@;
for($opt_f, $opt_l) {
$_ = [sort {$a <=> $b} split(/,/, $_)] if ($_);
}
my $l_idx;
my $l_next;
if ($opt_l) {
$l_idx=0; $l_next=$$opt_l[0];
}
my $f_idx;
my $f_next;
if ($opt_f) {
$f_idx=0; $f_next=$$opt_f[0];
}
my $ln=1;
my $fn=1;
while (<STDIN>) {
my $on_sep = /$opt_d/;
if (defined($l_idx) and $ln==$l_next) {
print;
++$l_idx;
if ($l_idx <= $#$opt_l) {
$l_next = $$opt_l[$l_idx];
} else {
$l_idx = undef;
}
} elsif (defined($f_idx) and $fn==$f_next and !$on_sep) {
print;
}
if ($on_sep) {
if (defined($f_idx) and $fn==$f_next) {
++$f_idx;
if ($f_idx <= $#$opt_f) {
$f_next = $$opt_f[$f_idx];
} else {
$f_idx = undef;
}
}
++$fn;
}
++$ln;
}
# TODO: multi-line delimiters
# TODO: start and end delimiters