help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Loop with files coming through sorted by name


From: tolugboji
Subject: Loop with files coming through sorted by name
Date: Sun, 17 Oct 2021 02:09:56 +0000

-‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Sunday, October 17th, 2021 at 1:11 AM, Dennis Williamson 
<dennistwilliamson@gmail.com> wrote:

> On Sat, Oct 16, 2021, 1:58 PM Daniel Mills danielmills1@gmail.com wrote:
>
> > On Sat, Oct 16, 2021 at 1:43 PM tolugboji tolugboji@protonmail.com
> >
> > wrote:
> >
> > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > >
> > > On Saturday, October 16th, 2021 at 5:32 PM, Daniel Mills <
> > >
> > > danielmills1@gmail.com> wrote:
> > >
> > > > On Sat, Oct 16, 2021 at 12:17 PM tolugboji tolugboji@protonmail.com
> > > >
> > > > wrote:
> > > >
> > > > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > > > >
> > > > > On Saturday, October 16th, 2021 at 1:31 PM, Greg Wooledge <
> > > > >
> > > > > greg@wooledge.org> wrote:
> > > > >
> > > > > > On Fri, Oct 15, 2021 at 08:31:04PM -0700, Seth David Schoen wrote:
> > > > > >
> > > > > > > You can thus do
> > > > > > >
> > > > > > > readarray -d "" myarray < <$(find . -type f -print0 | sort -zn)
> > > > > > >
> > > > > > > for i in "${myarray[@]}"; do
> > > > > > >
> > > > > > > arbitrary shell stuff with "$i"
> > > > > > > ===============================
> > > > > > >
> > > > > > > done
> > > > > >
> > > > > > You've got an extraneous $ in there. It's <(find...) not
> > > > > >
> > > > > > <$(find...).
> > >
> > > > > In general, files are described by
> > > > >
> > > > > ${fnam}-NUM.png
> > > > >
> > > > > Would it be possible to sort the files by number if I have the
> > > > >
> > > > > situation
> > > >
> > > > > schimmel-1.png
> > > > >
> > > > > schimmel-2.png
> > > > >
> > > > > ...
> > > > >
> > > > > schimmel-13.png
> > > > >
> > > > > The number of png files could be in the thousands.
> > > >
> > > > find ... -print0 | sort -zt '-' -k 2,2n | while IFS= read -rd '' file;
> > > >
> > > > do
> > >
> > > > ...
> > >
> > > Almost there except that there could be a number of '-' separators in the
> > >
> > > filename such as
> > >
> > > 2021-schimmel-title-1.png
> > >
> > > 2021-schimmel-title-2.png
> > >
> > > ...
> > >
> > > 2021-schimmel-title-356.png
> > >
> > > and the sorting would always occur on the last field.
> >
> > Ok, so count the fields and figure out which one you need? Grab one
> >
> > filename, count the "-"s in it, and use that in a variable. files=(*.png);
> >
> > tmp=${files[0]//[!-]}; field=$((${#tmp}+1)); printf '%s\0' "${files[@]}" |
> >
> > sort -zt '-' -k "$field"n | while IFS= read -rd '' file; do ... Or just
> >
> > 0-pad all your numbering so a basic lexographic sort handles all of this
> >
> > automatically and you can just use a glob.
> >
> > > Additionally I would also like to handle situations like this one
> > >
> > > 2021-schimmel-title-001.png
> > >
> > > 2021-schimmel-title-002.png
> > >
> > > ...
> > >
> > > 2021-schimmel-title-356.png
> >
> > The numeric sort will handle this already.
>
> A lexical sort will also handle a zero-padded fixed-field-width format.

Things work out on the following example list

/home/flora/edvart/docs/schimmel-01.png
/home/flora/edvart/docs/schimmel-02.png
/home/flora/edvart/docs/schimmel-03.png
/home/flora/edvart/docs/schimmel-04.png
/home/flora/edvart/docs/schimmel-05.png
/home/flora/edvart/docs/schimmel-06.png
/home/flora/edvart/docs/schimmel-07.png
/home/flora/edvart/docs/schimmel-08.png
/home/flora/edvart/docs/schimmel-09.png
/home/flora/edvart/docs/schimmel-10.png
/home/flora/edvart/docs/schimmel-11.png
/home/flora/edvart/docs/schimmel-12.png
/home/flora/edvart/docs/schimmel-13.png

Here is the code in use

for fl in "$@"; do
   fnme=${fl##*/}
   ftyp=${fl##*.}

   fdir=${fl%/*}
   fnam=${fnme%.*}

   nf=$( echo "$fnam" | awk -F '-' '{print NF}')
   ifld=$(( nf + 1 ))

   find "$fdir" -type f -name "${fnam}-*.png"  |
     sort -znt '-' -k "$ifld"n  |
   while IFS= read -r flimg
   do
     echo "$flimg"
   done
done

But when I try with the first three files being-1, -2, -3, the result is still 
not sorted numerically properly

/home/flora/edvart/docs/schimmel-04.png
/home/flora/edvart/docs/schimmel-05.png
/home/flora/edvart/docs/schimmel-06.png
/home/flora/edvart/docs/schimmel-07.png
/home/flora/edvart/docs/schimmel-08.png
/home/flora/edvart/docs/schimmel-09.png
/home/flora/edvart/docs/schimmel-10.png
/home/flora/edvart/docs/schimmel-11.png
/home/flora/edvart/docs/schimmel-12.png
/home/flora/edvart/docs/schimmel-13.png
/home/flora/edvart/docs/schimmel-1.png
/home/flora/edvart/docs/schimmel-2.png
/home/flora/edvart/docs/schimmel-3.png

I have a hunch that sort could be picking NUM.png and then fails to perform a 
proper numeric sort.








reply via email to

[Prev in Thread] Current Thread [Next in Thread]