Sort files by filename

Mikkel L. Ellertson mikkel at infinity-ltd.com
Tue Jul 31 21:59:21 UTC 2007


Les wrote:
> On Tue, 2007-07-31 at 15:12 -0400, Mark Haney wrote:
> Sort will give you the list.  I don't know about sorting on a substring
> with a command other than creating one.  In C, you could read the
> directory, then choose the substring using parsing, and finally look at
> the last two characters prior to the period to get the sequence and look
> for missing files.  Do you know the first number and last number?  If
> not, then this won't work, because the first file and last file would
> not have partners on each side to help you figure out if it was
> missing.  
> 
The -k and -t options of sort may do what is needed. If you want to
sort starting with the 5 character of the name, you could probably
do something like sort -k 1.5 as long as there are no spaces in the
name. I believe something like -k 1.5,1.7 would use the 5th, 6th,
and 7th character. Now, if there is a field separator between parts
of the file name, you can use the -t option to use that to separate
the name into fields. This is handy when the names are not fixed
length. Something like 10-12-07_MKE_10.14_XX.jpg could be split
using _ as the field seperator, and sorting on the 4th field. You
may have to specify a numeric sort, especially if the numbers do not
have leading zeros.

sort -n -t _ -k 4

Mikkel
-- 

  Do not meddle in the affairs of dragons,
for thou art crunchy and taste good with Ketchup!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20070731/d39c32e0/attachment-0001.sig>


More information about the fedora-list mailing list