Liste des Groupes | Revenir à cu shell |
On 18.06.2024 15:32, Chris Elvidge wrote:I tested in ksh - works as written.On 14/06/2024 at 08:31, Janis Papanagnou wrote:Yes, Ksh supports both, indexed and associative arrays.I'm using ksh here...>
>
I can set the shell parameters in numerical order
>
$ set {1..100}
>
then sort them _lexicographically_
>
$ set -s
>
Or do both in one
>
$ set -s {1..100}
>
I haven't found anything to sort them _numerically_ in shell.
>
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.
>
Setting the files as shell arguments with P*.HTM will also produce
lexicographical order.
>
The preceding files are just samples. It should work also if the
numbers are non-consecutive (say, 2, 10, 10000, 3333333) so that
iterating using a for-loop and building the list is not an option.
>
(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'?)
>
>
But the primary question is; how to organize/iterate the arguments
*numerically* _in shell_? (If that's possible in some simple way.)
>
>
N.B.: I prefer not to use external commands like 'sort' because of
the negative side effects and bulky code to handle newlines and
blanks in filenames, and messing around with quotes.
>
Janis
>
Can you use an array? E.g. (bash, I don't know ksh, but could be similar)
>One thing that concerns me with arrays is that I seem to recall that
for i in P1.HTM P10.HTM P11.HTM P2.HTM P3.HTM P4.HTM P5.HTM P6.HTM
P7.HTM P8.HTM P9.HTM; do
j=${i//[![:digit:]]}
files[j]="$i"
done
printf '%s\n' "${files[@]}"
P1.HTM
P2.HTM
P3.HTM
P4.HTM
P5.HTM
P6.HTM
P7.HTM
P8.HTM
P9.HTM
P10.HTM
P11.HTM
>
I'll have to work on names with two (or more?) numbers.
there was a limit in the number of array elements (which might be an
issue on lengthy lists of files). But some ad hoc tests seem to show
that if there's a limit it's not any more in the 1k/4k elements range
as it had been. (Bolski/Korn says their arrays support at least 4k.)
Janis
Les messages affichés proviennent d'usenet.