Re: Numerically sorted arguments (in shell)

Liste des GroupesRevenir à cu shell 
Sujet : Re: Numerically sorted arguments (in shell)
De : chris (at) *nospam* slack15-a.mshome.net (Chris Elvidge)
Groupes : comp.unix.shell
Date : 21. Jun 2024, 15:21:43
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <7cbgkkx149.ln2@slack15-a.local.uk>
References : 1 2 3 4 5 6
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1
On 20/06/24 00:45, vallor wrote:
On Wed, 19 Jun 2024 16:06:37 +0100, Chris Elvidge <chris@x550c.mshome.net>
wrote in <v4us5u$21bu3$1@dont-email.me>:
 
On 19/06/2024 at 14:11, Janis Papanagnou wrote:
On 19.06.2024 14:40, Chris Elvidge wrote:
On 18/06/2024 at 18:04, Janis Papanagnou wrote:
I've just tried a Unix tools based solution (with sed, sort, cut).
[...]
[...], 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).
>
>
If you're happy not handling pathological filenames:
>
Well, typically I can indeed ignore them. But it's better of course to
avoid situations where processing is compromised by such names.
>
>
for (( i=1; i<=50; i++ )); do for (( j=2; j<=120; j+=3 )); do touch
"a${i}b${j}c.txt"; done; done to create the files.
>
exnums() { j="$(sed 's/[^[:digit:]]\+/ /g' <<<"$@")"; printf '%s%s\n'
"$j" "$@"; }
function replaces all non-digit sequences with a space, prints digit
sequence(s) and original input.
>
for i in *; do exnums "$i"; done | sort -k1n -k2n -k3n -k4n | awk
'{print $NF}'
sort doesn't seem to care how many -k you use, fields separated with
space.
awk prints the last field of the input.
>
This "seems" to work with all manner of filenames from PNN.htm (as
your original sequence) to p323dc45g12.htm, p324dc45g12.htm,
p333dc45g12.htm Seems to work in ksh, too.
>
I tried the approach I outlined above... (here just echo'ing the
created parts)...
>
>
N=${1:-1}
sed_a="[^0-9]*\([0-9]\+\)[^0-9]*"
sed_r="\1\t"
sort_a="-k1n"
for (( n=2; n<=N; n++ ))
do
      sed_a+="\([0-9]\+\)[^0-9]*"
      sed_r+="\${n}\t" sort_a+=" -k${n}n"
done cut_a="-f$((N+1))-"
>
echo "# The following commands would be connected by pipes:"
echo "sed 's/${sed_a}/${sed_r}&/'"
echo "sort -t$'\t' ${sort_a}"
echo "cut ${cut_a}"
>
>
Janis
>
>
Your way is still restricted to filenames with a known number of sets of
digits, though (AFAICS). I.e. you pass N rather than finding it.
>
But it takes a long time to do it my way, a call to sed for each
filename, so I tried to cut down the time taken to do this and came up
with:
>
bash: exnums() { shopt -s extglob; j="${@//+([^[:digit:]])/ }"; printf
'%s%s\n' "$j" "$@"; }
>
ksh: exnums() { j="${@//+([^[:digit:]])/ }"; printf '%s%s\n' "$j" "$@";
}
>
ksh seems to do the extglob needed for bash natively.
>
removing the sed calls from exnum changes the time taken from 37 secs to
under 1 sec with 2000+ files ksh is faster than bash, ksh 50% of the
bash time taken.
>
Substituting a tab for the replacement space in j= and -t$'\t' in sort
would seem to allow spaces in filenames, too, as you originally had it.
 I finally remembered which tool has "versionsort(3)" -- it's ls:
 $ ls -1
test10.txt
test1.txt
test2.txt
 $ ls -v -1
test1.txt
test2.txt
test10.txt
 Does that help?
 
I didn't realise it could work like that. Thanks.
--
Chris Elvidge
England

Date Sujet#  Auteur
14 Jun 24 * Numerically sorted arguments (in shell)46Janis Papanagnou
15 Jun 24 +* Re: Numerically sorted arguments (in shell)2Axel Reichert
15 Jun 24 i`- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
16 Jun 24 +* Re: Numerically sorted arguments (in shell)20Lawrence D'Oliveiro
16 Jun 24 i`* Re: Numerically sorted arguments (in shell)19Janis Papanagnou
16 Jun 24 i +* Re: Numerically sorted arguments (in shell)14Lawrence D'Oliveiro
16 Jun 24 i i`* Re: Numerically sorted arguments (in shell)13Janis Papanagnou
16 Jun 24 i i +* Re: Numerically sorted arguments (in shell)4Helmut Waitzmann
17 Jun 24 i i i+- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
18 Jun 24 i i i`* Re: Numerically sorted arguments (in shell)2Geoff Clare
19 Jun 24 i i i `- Re: Numerically sorted arguments (in shell)1Helmut Waitzmann
17 Jun 24 i i `* Re: Numerically sorted arguments (in shell)8Lawrence D'Oliveiro
17 Jun 24 i i  `* Re: Numerically sorted arguments (in shell)7Janis Papanagnou
17 Jun 24 i i   `* Re: Numerically sorted arguments (in shell)6Lawrence D'Oliveiro
17 Jun 24 i i    `* Re: Numerically sorted arguments (in shell)5Janis Papanagnou
18 Jun 24 i i     `* Re: Numerically sorted arguments (in shell)4Lawrence D'Oliveiro
18 Jun 24 i i      +- Re: Numerically sorted arguments (in shell)1Kenny McCormack
18 Jun 24 i i      +- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
19 Jun 24 i i      `- Re: Numerically sorted arguments (in shell)1Kaz Kylheku
16 Jun 24 i `* Re: Numerically sorted arguments (in shell)4Eric Pozharski
17 Jun 24 i  `* Re: Numerically sorted arguments (in shell)3Janis Papanagnou
18 Jun 24 i   `* Re: Numerically sorted arguments (in shell)2Eric Pozharski
19 Jun 24 i    `- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
18 Jun 24 +* Re: Numerically sorted arguments (in shell)5Chris Elvidge
18 Jun 24 i`* Re: Numerically sorted arguments (in shell)4Janis Papanagnou
18 Jun 24 i `* Re: Numerically sorted arguments (in shell)3Chris Elvidge
18 Jun 24 i  `* Re: Numerically sorted arguments (in shell)2Janis Papanagnou
18 Jun 24 i   `- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
18 Jun 24 `* Re: Numerically sorted arguments (in shell)18Janis Papanagnou
19 Jun 24  `* Re: Numerically sorted arguments (in shell)17Chris Elvidge
19 Jun 24   `* Re: Numerically sorted arguments (in shell)16Janis Papanagnou
19 Jun 24    `* Re: Numerically sorted arguments (in shell)15Chris Elvidge
20 Jun 24     +* Re: Numerically sorted arguments (in shell)11vallor
20 Jun 24     i+- Re: Numerically sorted arguments (in shell)1Janis Papanagnou
20 Jun 24     i+* Re: Numerically sorted arguments (in shell)6Janis Papanagnou
21 Jun 24     ii`* Re: Numerically sorted arguments (in shell)5vallor
21 Jun 24     ii `* Re: Numerically sorted arguments (in shell)4Lawrence D'Oliveiro
21 Jun 24     ii  `* Re: Numerically sorted arguments (in shell)3vallor
21 Jun 24     ii   `* Re: Numerically sorted arguments (in shell)2Lawrence D'Oliveiro
21 Jun 24     ii    `- Re: Numerically sorted arguments (in shell)1Kenny McCormack
21 Jun 24     i`* Re: Numerically sorted arguments (in shell)3Chris Elvidge
24 Jun 24     i `* Is 'ls -v' the Final Solution? (Was: Numerically sorted arguments (in shell))2Kenny McCormack
24 Jun 24     i  `- Re: Is 'ls -v' the Final Solution? (Was: Numerically sorted arguments (in shell))1Janis Papanagnou
20 Jun 24     `* Re: Numerically sorted arguments (in shell)3Janis Papanagnou
24 Jun 24      `* Re: Numerically sorted arguments (in shell)2Chris Elvidge
24 Jun 24       `- Re: Numerically sorted arguments (in shell)1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal