make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / lines
blob9e1f0e651128b0aa0efd7105249076fc0c3f03b2
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 lines - Output only the given lines of the input stream
9 =head1 SYNOPSIS
11 lines [I<RANGES> [I<RANGES> [...]]] [-- I<FILE> [I<FILE> [...]] | < I<FILE>]
13 =head1 DESCRIPTION
15 Read from from I<FILE>s if specified, STDIN otherwise.
16 I<RANGES> is comma-delimited list of line numbers and inclusive ranges.
17 Special word "B<EOF>" in a range's upper limit represents the end of the file.
19 Starts the line numbering from 1.
21 If multiple files are given, restart the line numbering on each file.
23 Always displays the lines in the in-file order, not in arguments-order,
24 how they were given in I<RANGES> arguments; ie. does not buffer or seek in the input files.
25 So I<lines 1,2> and I<lines 2,1> both display the 1st line before the 2nd.
27 =head1 EXAMPLES
29 =over 4
31 =item lines 1
33 =item lines 2-10
35 =item lines 1,5-10 3
37 =item lines 2-4 6,8 10-EOF
39 =back
41 =head1 EXIT STATUS
43 Exit 2 if there was a range which was not found,
44 ie. a file had less lines than requested.
46 =cut
48 use Data::Dumper;
49 use Pod::Usage;
50 use List::MoreUtils qw/all any none/;
53 if(grep {$_ eq '--help'} @ARGV)
55 pod2usage(-exitval=>0, -verbose=>99);
58 my @ranges;
60 while(exists $ARGV[0])
62 if($ARGV[0] eq '--')
64 shift @ARGV;
65 last;
67 for my $range (split /,/, $ARGV[0])
69 if($range =~ /^(\d+)(?:-(\d+|EOF))?$/)
71 push @ranges, {'from'=>$1, 'to'=>$2, 'found'=>1};
73 else
75 die "invalid range: $range\n";
78 shift @ARGV;
81 # load this delayed
82 eval 'use ARGV::readonly; 1' or die;
84 $status = 0;
86 while(<>)
88 if($ARGV ne $prev_ARGV)
90 $. = 1;
91 $status = 2 if any {$_->{'found'} != 1} @ranges;
92 map {$_->{'found'} = 0} @ranges;
94 my $include = 0;
95 for my $range (@ranges)
97 my $from = $range->{'from'};
98 my $to = $range->{'to'};
99 if(defined $to)
101 if($. >= $from and ($to eq 'EOF' or $. <= $to)) { $range->{'found'} = 1; $include = 1; }
103 else
105 if($. == $from) { $range->{'found'} = 1; $include = 1; }
108 if($include)
110 print;
112 $prev_ARGV = $ARGV;
115 $status = 2 if any {$_->{'found'} != 1} @ranges;
117 exit $status;