Liste des Groupes | Revenir à cu shell |
On 14/06/2024 at 08:31, Janis Papanagnou wrote: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)
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.
Les messages affichés proviennent d'usenet.