Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:39:33+00:00 2026-05-24T09:39:33+00:00

Problem: Need to sort array before operating on them with function. First, array is

  • 0

Problem:

Need to sort array before operating on them with function.

First, array is loaded with files:

unset a i
counter=1
while IFS= read -r -d $'\0' file; do
    a[i++]="$file"
done < <(find $DIR -type f -print0)

Next, each member of array is sent to function

for f in "${a[@]}"
do
   func_hash "$f"
   [ $(expr $counter % 20) -eq 0 ] && printf "="
   counter=$((counter + 1))
done

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.

Ideas?

Thanks!

Bubnoff

UPDATE: Here’s code with sort:

while IFS= read -r -d $'\0' file; do
    func_hash "$file"
    [ $(expr $counter % 20) -eq 0 ] && printf "="
    counter=$((counter + 1))
done < <(find $DIR -type f -print0 | sort -z +1 -1)

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?

UPDATE 2: Decided to compromise.

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:

sort -o $OUT -t',' -k 1 $OUT

Anyone have a more ‘elegant’ solution ( whatever that means ).

SOLVED See jw013’s answer below. Thanks man!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:39:34+00:00Added an answer on May 24, 2026 at 9:39 am

    EDIT

    while IFS= read -r -d/ && read -r -d '' file; do
        a[i++]="$file"
    done < <(find "$DIR" -type f -printf '%f/%p\0' | sort -z -t/ -k1 )
    

    Rationale:

    • I make the assumption that / is never a legal character within a file name (which seems reasonable on most *nix filesystems since it is the path separator).
    • The -printf is used to print the file name without leading directories, then then full file name with path, separated by /. The sort takes place on the first field separated by / which should be the full file name without path.
    • The read is modified to first use / as a delimiter to throw out the pathless file name.

    side note

    Any POSIX shell should support the modulo operator as part of its arithmetic expansion. You can replace line with the call to external command expr in the second loop with

    [ $(( counter % 20 )) -eq 0 ] ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need make a merge sort using an additional array. Here is my code:
I have a PHP array that I need to sort. I have included the
I have a huge array of ints that I need to sort. The catch
I need to sort an array containing a list of words and search the
The Problem: I need to receive different types of content from a number of
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
This is my problem: I need to store a lot of log messages and
To solve some problem I need to compute a variant of the pascal's triangle
I faced a problem - I need to use a macro value both as
I have the following problem: We need to find the next august. I other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.