Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls -l | sort on 1st and then 3rd field

Status
Not open for further replies.
Feb 12, 2002
80
NO
Hi,

I have a series of files on disk that I am trying to list sorted by the 1st, then the 3rd field (field delim being a full stop/period ".")

For example:
C00014403.txt.1
C00014403.txt.2
C00014403.txt.3
C00014403.txt.4
C00014403.txt.5
C00014403.txt.6
C00014404.txt.1
C00014404.txt.2
C00014404.txt.3
C00014404.txt.4
C00014404.txt.5
C00014404.txt.6

I have no control over the file extension, so canot make it 001, 002, and these increment upto 200+.

I have tried using the sort command as follows:

ls -l | sort -t . -k 1.2n,3n

But this is not working.

How can sort my files so that they display as I want them to?

I can not use the time stamp either.


Any ideas?!

Thanks,
littleIdiot

 
So I just tried again with the following and it works fine:

ls -l | sort -t . -k 1,3

or I guess

ls -l | sort -t . -k 1,3n


This replies on field 2 having no effect on the sort - if anyone sees a better way then feel free to reply!

Cheers,
lI






 
For this I would use ls -l | sort -t. -k 1,1 -k 3,3n, although it makes no difference to the results in your example. -k 1,3 implies that the sort key is fields 1 to 3 inclusive, whereas -k 1,1 -k 3,3n means that the file is sorted first by field 1, and then by field 3 (in the cases where the values of field 1 are identical).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top