|
(Chrono)logical Sorting |
Sort file names chronologically and move up the directory hierarchyBy Becca ThomasThe way Unix sorts files can sometimes be a frustrating thing. You may wonder why it can't recognize that files named for months should be arranged in chronological order rather than alphabetical order. The file whose name begins with ``dec'' should follow the file named ``nov,'' not precede it, but you have to tell Unix how to do it. Robert Nicholson provides a Perl utility that is useful for sorting file names chronologically instead of the default alphabetical sorting order used by Unix utilities. The approach provides a template for sorting other file-name formats. David Wood submits a small shell function that provides a command-line shorthand for ascending the directory hierarchy. By the DatesDear Dr. Thomas: I recently wanted to scan some past Wizard's Grabbag listings,
so I set up the I couldn't rely on the files' modification time and just use
The key is to sort the list by date rather than alphabet. I
thought about the language features needed to solve this problem.
The first thing I considered was the C language The result is the Explanation of Lines 18-22 save the command-line arguments that begin with a
mmmyy prefix (such as jun92 or aug94). In particular,
the Line 24 calls a sort routine that orders files names stored in
the Two arguments are passed to the comparator function (lines 28-44), which returns -1, 0, or 1 depending on whether the first value is less, equal to, or greater than the second. Perl passes these values by reference and their pointers are swapped accordingly. The function works by associating any month's three-character prefix with its corresponding integer value. This value is then catenated with the two-digit year part of the file name, and a numerical comparison is made to determine whether the first value is greater than, less than, or equal to the second. To support UnixWorld's current naming scheme, a significant limitation was introduced: The program assumes a two-digit year and will not function when we hit the year 2000. However, it would be a trivial modification to support four-digit year values. The month's integer value is adjusted to ensure that we always have a two-digit month value. Consider what happens when we compare the dates dec91 and jan94. If we didn't adjust the value, these would translate into 9112 and 941, respectively. Then, when the values are compared, the first date (dec91) would have been regarded as more recent than the second (jan94). Adding 11 to each month's integer value ensures that we are always comparing four-digit values. Now let's look at the Lines 37-38 convert the month substring to its equivalent
ordinal value (for example, jan = 1 and dec = 12). Here the
Lines 41-42 create the integer values used for the comparison by line 43. Eleven is added to each month index value so it will always be a two-digit value. The two-digit year value is then catenated to the adjusted month value in order to yield the desired four-digit number. What's that funny looking operator ( What about the Tester's CommentsMy two-line hack does something similar [see Listing 1C]. Of course, it's not as
portable as a Perl program because the Up the HierarchyDear Dr. Thomas: I've enclosed a shell function named up that I find handy to
move one or more directories toward the root directory. This
simple function takes one optional argument, which specifies how
many levels to ascend. Thus, instead of entering
This Bourne-shell-compatible function was developed using Korn shell version 88 and tested using the Bourne shell running under System V Release 3.2.3 on a 3B2/1000. David Wood / Cliffwood, N.J.Portability Note: Shell functions are available starting with the Bourne shell that was shipped with System V Release 2 and all versions of the Korn shell. Caution: If you've defined a Korn shell alias using the same name as our function, you'll get a syntax error when you attempt to define the function. In this case you'll need to either rename the alias or the function. Notes: Part A of Listing
2 shows the Tester's CommentsThe Bourne shell version works fine. I couldn't test the Korn
shell version (might be time to install CorrectionsSorry, we goofed. Last month we published two domain names
with typos. The domain name Sorry for any confusion. |
Print This Page Send as e-mail |












