Deutsch English Français Italiano |
<v4ule0$200am$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: Wed, 19 Jun 2024 15:11:27 +0200 Organization: A noiseless patient Spider Lines: 61 Message-ID: <v4ule0$200am$1@dont-email.me> References: <v4grk8$2pp51$1@dont-email.me> <v4sena$1eqki$1@dont-email.me> <v4ujjt$1vjpg$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 19 Jun 2024 15:11:28 +0200 (CEST) Injection-Info: dont-email.me; posting-host="4b708d1a279efe9cda7a957a2de8b2c6"; logging-data="2097494"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/M2I6q/SZrfffSZTW9pcxS" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:/AVcCo+FYLgAFSWXTBTonvA5HtA= In-Reply-To: <v4ujjt$1vjpg$1@dont-email.me> Bytes: 3012 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