| Deutsch English Français Italiano |
|
<v4sena$1eqki$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.unix.shell
Subject: Re: Numerically sorted arguments (in shell)
Date: Tue, 18 Jun 2024 19:04:41 +0200
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <v4sena$1eqki$1@dont-email.me>
References: <v4grk8$2pp51$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 18 Jun 2024 19:04:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5688cbe9191d9f2c921f13829f6efad2";
logging-data="1534610"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19M0kfuG5xePELeqTIfzZB/"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:u30VC7oYK13naxh3SAeCS7QsX1s=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <v4grk8$2pp51$1@dont-email.me>
Bytes: 3434
I've just tried a Unix tools based solution (with sed, sort, cut).
Up to and including the line containing 'shuf' is data generation,
the rest (starting with 'sed') extracts and sorts the data. I've
written it for TWO numeric sort keys (see printf format specifier)
for (( i=1; i<=50; i++ ))
do
for (( j=2; j<=120; j+=3 ))
do
printf "a%db%dc.txt\n" i j
done
done |
shuf |
sed 's/[^0-9]*\([0-9]\+\)[^0-9]*\([0-9]\+\)[^0-9]*/\1\t\2\t&/' |
sort -t$'\t' -k1n -k2n |
cut -f3-
For just one numeric argument this can be simplified (shorter sed
pattern, simpler sort -n command), and for more than two numeric
fields it can be modified to dynamically construct the sed pattern,
the sort option list, and the cut parameter, once at the beginning;
that way we could have a tool for arbitrary amounts of numeric keys
in the file name.
Note: this program doesn't handle pathological filenames (newlines).
Janis
On 14.06.2024 09: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
>