View previous topic :: View next topic |
Author |
Message |
Fred Krogh Veteran
Joined: 07 Feb 2005 Posts: 1036 Location: Tujunga, CA
|
Posted: Fri Mar 05, 2010 2:42 pm Post subject: ls and sorting -- SOLVED |
|
|
ls no longer sorts names the way it used to, and it has fouled up some scripts of mine. The first sign of problems for me was caused by sorting dprpl.f after dprpl1.f. But after seeing the sort order divadb.f,
diva.f, divag.f, I'm at a loss to know what to expect from ls. Does anyone know how this works or of an alternative to ls to get a sorted list of names into a file. Thanks,
Fred
Last edited by Fred Krogh on Fri Mar 05, 2010 3:17 pm; edited 1 time in total |
|
Back to top |
|
|
Frustie Tux's lil' helper
Joined: 31 Aug 2007 Posts: 102 Location: My own little planet.
|
Posted: Fri Mar 05, 2010 3:14 pm Post subject: |
|
|
there are a couple of different ways of sorting with ls, see man ls :
Code: | --sort=WORD
sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v
|
but you could also pipe the output of ls through sort
# ls | sort
see man sort for more options. |
|
Back to top |
|
|
Fred Krogh Veteran
Joined: 07 Feb 2005 Posts: 1036 Location: Tujunga, CA
|
Posted: Fri Mar 05, 2010 3:17 pm Post subject: |
|
|
Thanks, passing the output through sort does the job.
LC_ALL=C ls ...
also works. Thanks. |
|
Back to top |
|
|
i92guboj Bodhisattva
Joined: 30 Nov 2004 Posts: 10315 Location: Córdoba (Spain)
|
Posted: Fri Mar 05, 2010 5:19 pm Post subject: Re: ls and sorting -- SOLVED |
|
|
Fred Krogh wrote: | ls no longer sorts names the way it used to, and it has fouled up some scripts of mine. The first sign of problems for me was caused by sorting dprpl.f after dprpl1.f. But after seeing the sort order divadb.f,
diva.f, divag.f, I'm at a loss to know what to expect from ls. Does anyone know how this works or of an alternative to ls to get a sorted list of names into a file. Thanks,
Fred |
First, if you want your script to be reliable, you shouldn't be using ls at all for that purpose. I'll spare you the explanation and post a link instead, in case you are interested.
http://mywiki.wooledge.org/ParsingLs
Besides that, you are also assuming that the ls output will come given into a very specific format, which is even insaner being ls the top-one aliased and modded command line tool. You shouldn't be expecting ls to act at all in a given way. It will vary from system to system. I can't be too specific without knowing anything about your script though. |
|
Back to top |
|
|
Fred Krogh Veteran
Joined: 07 Feb 2005 Posts: 1036 Location: Tujunga, CA
|
Posted: Fri Mar 05, 2010 5:26 pm Post subject: |
|
|
Thanks for this. In my case, there is no chance that names will contain anything other than letters numbers and a single '.'. It is also very doubtful that the script will be run on any other machine, but of course the future can hold surprises. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|