Sujet : Re: Numerically sorted arguments (in shell)
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.unix.shellDate : 15. Jun 2024, 16:43:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4k9b8$3go8u$1@dont-email.me>
References : 1 2
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 15.06.2024 15:56, Axel Reichert wrote:
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
What I'm trying to do is iterating over files, say,
P1.HTM P10.HTM P11.HTM P2.HTM P3.HTM P4.HTM P5.HTM P6.HTM P7.HTM
P8.HTM P9.HTM
in numerical order.
Could you employ printf to add leading zeros, sort lexicogpaphically and
then remove the zeros?
Something like this (not using printf but a more elementary method) is
actually what I'm currently doing. (It's not really complex but quite
some data fiddling I wanted to avoid. I have that in a script and it's
not a general solution but handles just simple cases like the sample
data above, which is 95% of my usages so it's okay for that but not
really satisfying for other or more general cases.)
To get the original name back I think I'd have to store the original
names along with the new names. (Which is something I've not yet done.)
(Ideally I'd also like to handle names with two numbers "A35P56.txt"
and irregular string components (lowercase, say, "page310ch1.txt"),
but that's just a nice-to-have. - I might make use of 'sort'?)
You did not yet mention what your final goal is with the numerically
sorted list.
The original application was that I simply wanted to sequentially skim
through a number of files.[*] In the past (where possible) I've just
renamed the files to let them have numbers of equal length (as noted
above). But the general task I envision is that I don't want to change
any name of data files but just be able to iterate over these files,
or list them numerically sorted (and without the known issues of \n
and blank handling).
I thought that a contemporary shell would probably support that but I
was astonished that (at least in ksh) it wasn't supported (as far as
I saw).[**]
In case this is in the end a renaming task, for this level of complexity
I would use the "wdired" mode of Emacs ("write directory edits") and use
regexes for search and replace. Or some other "multi-rename" tools from
the command line.
I've my own script to adjust numbers in files. But as said, I'd rather
want to iterate or sort, like the lexicographic ordering in ksh
set -s page*.gif
(which in that example is anyway the default for wildcard patterns)
something similar for numeric argument setting (or pattern expansion)
set --numerical page*.gif
To use that features more widely it would be nice if the wild-card
expansion could be controlled, say by
set -o numerical
Well, maybe that all makes no sense and should be tackled differently?
But it's how it appears to me at the moment. (Feel free to enlighten
me. :-)
Janis
[*] I occasionally have this task; the last time was when I wanted to
read old typewriter documents that had been scanned page-wise as GIF
files.
[**] Yet I haven't checked Zsh; that shell supports some non-standard
modifiers in certain zsh-specific constructs, so it might possible it
has support for this requirement as well. (But Zsh is not the shell
I'm using so I'm primarily seeking for a Ksh solution or POSIX shell
workaround.)