#!/usr/bin/perl -w # # macFilenameSort # # 2007-09-007: Written by Steven J. DeRose, sderose@acm.org. use strict; use Getopt::Long; my $version = "2010-09-12"; my $quiet = 0; my $verbose = 0; # Process options Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help|?" => sub { system "perldoc macFilenameSort"; exit; }, "q|quiet!" => \$quiet, "v|verbose+" => \$verbose, "version" => sub { die "Version of $version, by Steven J. DeRose.\n"; } ); ($result) || die "Bad options.\n"; ############################################################################### my @fwidths = (); my @records = (); my @keys = (); # Read all the input, extract first token of each line, and parse at digit-groups. while (my $l = <>) { chomp $l; # Extract up to first whitespace (my $key = $l) =~ s/\s.*$//; my @fields = split(/(\d+)/, $key); # Measure the field widths and keep track of max for each field for (my $i=0; $i $fwidths[$i]) { $fwidths[$i] = length($f); } } push @records, $l; push @keys, join("\t",@fields); } # Now go through and pad the fields for (my $r=0; $r 0) { $s .= ($padchar x $needed); } return($s); } sub lpad { my $s = $_[0]; my $len = $_[1]; if ($len <= 2) { $len = 12; } my $padchar = $_[2]; if ($padchar eq "") { $padchar = " "; } my $needed = $len - length($s); if ($needed > 0) { $s = ($padchar x $needed) . $s; } return($s); } ############################################################################### sub showUsage() { warn " =head1 Usage macFilenameSort Sort a list of files, treating numeric filenames right unlike *nix. Typically, pipe ls into this; perhaps also pipe this back out to 'wrap' to get back into multiple columns. =head1 Options =over =item * B<-q> Suppress most messages. =item * B<-v> Add more detailed messages. =item * B<-version> Display version info and exit. =back =head1 Known bugs and limitations Doesn't handle non-integers. =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 http://creativecommons.org/licenses/by-sa/3.0/. The author's present email is sderose at acm.org. For the most recent version, see http://www.derose.net/steve/utilities/. =cut "; }