#!/local/bin/perl # Routine to add numbers to the beginning of each line in a (presumably) # text file. # # Format: % linnum [-w digits] [ input_file [ output_file ] ] # # where -w digits indicates the width of the output line number field. # default is 5 digits. An additional space will also # be inserted between the number and the first byte # of the line. # # 24 Aug 1999, acr. # #========================================================================== # First, see if we have to open a file or if we've gotten a pipe: if (@ARGV > 4) { die "Usage: linnum [-w digits ] [ input_file [ output_file ] ]\n"; } $w = 5; $infile = "-"; $outfile = "-"; while (@ARGV > 0) { $a = shift(@ARGV); if ($a eq "-w") { $w = shift(@ARGV); } elsif ($infile eq "-") { $infile = $a; } elsif ($outfile eq "-") { $outfile = $a; } else { die "Usage: linnum [-w digits ] [ input_file [ output_file ] ]\n"; } } if ($w < 1) { die "Invalid integer width ($w).\n"; } # Open the I/O streams: open (IN,"$infile") || die "Could not open \"$infile\" for reading. "; open (OUT,">$outfile") || die "Could not open \"$outfile\" for writing. "; $count = 0; while ($line=) { $count++; printf OUT "%${w}.${w}d %s",$count, $line; } # Close the streams and we're done: close(IN); close(OUT);