#!/usr/bin/perl -w # # numberLines # # 2009-06-08: Written by Steven J. DeRose. # 2010-01-12 sjd: Add -width, -sep, -encoding, -hex, -ignore. # 2011-07-05 sjd: Clean up doc. # 2012-10-05 sjd: Cleanup, add -listEncodings. # # To do: # Option to colorize the number # use strict; use Getopt::Long; use Encode; our $VERSION = "2012-10-05"; my $iencoding = ""; my $hex = 0; my $ignore = ""; my $quiet = 0; my $delim = " "; my $verbose = 0; my $width = 6; ############################################################################### # my %getoptHash = ( "delim=s" => \$delim, "hex!" => \$hex, "h|help" => sub { system "perldoc $0"; exit; }, "iencoding=s" => \$iencoding, "ignore=s" => \$ignore, "listEncodings" => sub { warn "\nEncodings available:\n"; my $last = ""; my $buf = ""; for my $k (Encode->encodings(":all")) { my $cur = substr($k,0,2); if ($cur ne $last) { warn "$buf\n"; $last = $cur; $buf = ""; } $buf .= "$k "; } warn "$buf\n"; exit; }, "q!" => \$quiet, "v+" => \$verbose, "version" => sub { die "Version of $VERSION, by Steven J. DeRose.\n"; }, "width=i" => \$width, ); Getopt::Long::Configure ("ignore_case"); GetOptions(%getoptHash) || die("Bad options.\n"); ############################################################################### # if ($iencoding ne "") { binmode(STDIN, ":encoding($iencoding)") || die "Binmode failed for -iencoding '$iencoding'.\n"; print ""; binmode(STDOUT,":encoding($iencoding)"); } ############################################################################### ############################################################################### # Main # my $format = "%0$width" . ($hex ? "x":"d"); ($verbose) && warn "Format string: '$format'\n"; my $recnum = 0; while (my $rec = <>) { if ($ignore && $rec =~ /$ignore/) { print ((" " x $width) . $delim . $rec); } else { $recnum++; my $num = sprintf($format, $recnum); print "$num$delim$rec"; } } ($verbose) && warn "Handled $recnum lines.\n"; exit; ############################################################################### ############################################################################### ############################################################################### # =pod =head1 Usage numberLines [options] Print a line number before each line from stdin. =head1 Options =over =item * B<-delim> I Delimiter string to put between number and line (default: space). =item * B<-hex> Write the number in base 16 instead of base 10. =item * B<-iencoding> I Assume this character encoding for input. =item * B<-ignore> I Don't count or number lines matching the regex. =item * B<-listEncodings> Show all the encodings supported by I<-iencoding> and I<-oencoding>, and exit. =item * B<-q> Suppress most messages. =item * B<-v> More messages. =item * B<-version> Display version info and exit. =item * B<-width> I Zero-pad the number to this many digits. =back =head1 Related commands C is similar, but doesn't have I<-hex>, I<-width>, or support for various encodings. =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