new tool: args2env
[hband-tools.git] / user-tools / lines
blob4a9d76b13dbeca4d4503a33d0bdd2b7d435231a8
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 lines - Output only the given lines of the input stream
9 =cut
13 if(grep {$_ eq '--help'} @ARGV)
15 die "Usage: $0 [<RANGES> [<RANGES> [...]]] [-- <FILE> [<FILE> [...]]]
16 Read from from FILEs if specified, STDIN otherwise.
17 RANGES is comma-delimited list of line numbers and inclusive ranges.
18 Special symbol 'EOF' represents the end of the input file.
19 Examples:
21 2-10
22 1,5-10
23 2,4,6,8,10-EOF\n";
26 my @ranges;
28 while(exists $ARGV[0])
30 if($ARGV[0] eq '--')
32 shift @ARGV;
33 last;
35 for my $range (split /,/, $ARGV[0])
37 if($range =~ /^(\d+)(?:-(\d+|EOF))?$/)
39 push @ranges, [$1, $2];
41 else
43 die "invalid range: $range\n";
46 shift @ARGV;
49 # load this delayed
50 eval 'use ARGV::readonly; 1' or die;
52 while(<>)
54 $. = 1 if $ARGV ne $prev_ARGV;
55 my $include = 0;
56 for my $range (@ranges)
58 my ($from, $to) = @$range;
59 if(defined $to)
61 if($. >= $from and ($to eq 'EOF' or $. <= $to)) { $include = 1; }
63 else
65 if($. == $from) { $include = 1; }
68 if($include)
70 print;
72 $prev_ARGV = $ARGV;