Liste des Groupes |
On 19.06.2024 14:40, Chris Elvidge wrote: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.On 18/06/2024 at 18:04, Janis Papanagnou wrote:Well, typically I can indeed ignore them. But it's better of courseI'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:
to avoid situations where processing is compromised by such names.
>I tried the approach I outlined above... (here just echo'ing the
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.
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
Les messages affichés proviennent d'usenet.