#!/usr/bin/perl -w # # longlines: Find lines longer than a certain length. # # Written by Steven J. DeRose. # 2007-11-29 sjd: strict. # 2010-09-12 sjd: cleanup? # 2012-01-16 sjd: Clean up, support STDIN, don't use system() pipe. # # Todo: # Option to prepend actual line lengths to the lines (and ranklist). # use strict; use Getopt::Long; our $VERSION = "2012-01-16"; my $len = 80; my $loc = 0; my $quiet = 0; my $verbose = 0; ############################################################################### # Process arguments # Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help" => sub { system "perldoc $0"; exit; }, "len|n=i" => \$len, "loc" => \$loc, "q!" => \$quiet, "v+" => \$verbose, "version" => sub { die "Version of $VERSION, by Steven J. DeRose.\n"; }, ); ($result) || die "Bad options.\n"; if ($len <= 0) { warn "Line-length limit -len $len invalid.\n"; system "perldoc $0"; exit; } if (scalar(@ARGV) == 0) { push @ARGV, "-"; } ############################################################################### # my $recnum = 0; my $longOnes = 0; my $maxlen = 0; while (my $file = shift) { ($verbose) && warn "Starting file '$file'\n"; open(IFH, "<$file") || warn "Can't find input file '$file'.\n"; while (my $rec = ) { $recnum++; if (length($rec) > $maxlen) { $maxlen = length($rec); } if (length($rec) > $len) { $longOnes++; if ($loc) { printf("%06d: %s", $recnum, $rec); } else { print $rec; } } } close(IFH); } ($quiet) || print "Done: $recnum records, $longOnes over $len chars, longest = $maxlen.\n"; exit; ############################################################################### ############################################################################### # =pod =head1 Usage longLines [options] [files] Extracts all lines longer than a certain limit (default 80). =head1 Options =over =item * B<-len> I Set a different line-length limit. =item * B<-loc> Report only line numbers, not contents =item * B<-q> Suppress most messages. =item * B<-v> Add more messages. =back =head1 Related Commands C: count lines in file(s) by length. C will report the length of a file's longest line. =head1 Ownership This work by Steven J. DeRose is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. For further information on this license, see L. The author's present email is sderose at acm.org. For the most recent version, see L. =cut