Note that there are some explanatory texts on larger screens.

plurals
  1. POBASH: Sort array of files with crazy names
    text
    copied!<p>Problem:</p> <p>Need to sort array before operating on them with function.</p> <p>First, array is loaded with files:</p> <pre><code>unset a i counter=1 while IFS= read -r -d $'\0' file; do a[i++]="$file" done &lt; &lt;(find $DIR -type f -print0) </code></pre> <p>Next, each member of array is sent to function</p> <pre><code>for f in "${a[@]}" do func_hash "$f" [ $(expr $counter % 20) -eq 0 ] &amp;&amp; printf "=" counter=$((counter + 1)) done </code></pre> <p>Somehow a sort needs to be thrown into the above for loop. Have looked through the SO posts on sorting arrays but somehow my crazy file names cause issues when I try to tack on a sort.</p> <p>Ideas?</p> <p>Thanks!</p> <p>Bubnoff</p> <p><strong>UPDATE:</strong> Here's code with sort:</p> <pre><code>while IFS= read -r -d $'\0' file; do func_hash "$file" [ $(expr $counter % 20) -eq 0 ] &amp;&amp; printf "=" counter=$((counter + 1)) done &lt; &lt;(find $DIR -type f -print0 | sort -z +1 -1) </code></pre> <p>It's sorting by full path rather than file name. Any ideas on how to sort by file name given that the path is needed for the function?</p> <p><strong>UPDATE 2:</strong> Decided to compromise.</p> <p>My main goal was to avoid temp files using sort. GNU sort can write back to the original file with its '-o' option so now I can:</p> <pre><code>sort -o $OUT -t',' -k 1 $OUT </code></pre> <p>Anyone have a more 'elegant' solution ( whatever that means ).</p> <p><strong>SOLVED</strong> See jw013's answer below. Thanks man!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload