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

  • SEARCH
  • Home
  • 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 7988175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:21:38+00:00 2026-06-04T12:21:38+00:00

I want to put the result of the sort method into an array where

  • 0

I want to put the result of the sort method into an array where each cell contains a word. I tried this code but only part of the $file is printed and its not sorted:

#!/bin/bash
for file in `ls ${1}` ; do
    if [[ ! ($file = *.user) ]] ; then
        continue
    fi

    arr=(`sort -nrk4 $file`)
    echo ${arr[*]}

done

Why isnt this working? How can I do this?

Data file:

name1 01/01/1994 a 0
name2 01/01/1994 b 5
name3 01/01/1994 c 2

If I run the sort line only (sort -nrk4 $file), this is whats printed:

name2 01/01/1994 b 5
name3 01/01/1994 c 2
name1 01/01/1994 a 0

When I run the 2 lines above, this is what its printed:

 name1 01/01/1994 a 0
  • 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-06-04T12:21:39+00:00Added an answer on June 4, 2026 at 12:21 pm

    In order for each line of the sort output to be put into its own array element, IFS needs to be set to a newline. To output the array, you need to iterate over it.

    #!/bin/bash
    for file in $1; do
        if [[ ! $file = *.user ]]; then
            continue
        fi
    
        saveIFS=$IFS
        IFS=$'\n'
        arr=($(sort -nrk4 "$file"))
        IFS=$saveIFS
        for i in "${arr[@]}"
        do
            echo "$i"
        done
    
    done
    

    Alternatively, don’t reset IFS, don’t use a loop for output and the following command will output the elements separated by newlines since the first character of IFS is used for the output separator when * is used for the subscript in a quoted context:

    echo "${arr[*]}"
    

    Remember, quoting fixes everything.

    Don’t use ls in a for loop. If $1 may contain globbing, then it should be left unquoted. It not, then it should be quoted in case the directory or filename may contain whitespace characters. Ideally, the contents of $1 should be validated before they’re used here.

    The comparison operators have higher precedence than the negation operator so the parentheses are unnecessary.

    For command substitution, $() is preferred over backticks. They’re more readable and easier to use nested.

    And finally, when variables are expanded, the should be quoted. Also, @ should almost always be used to subscript arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to put the result of this: std::tr1::mem_fn(&ClassA::method); Inside a variable, what is
I want to sort my lucene(.net) search results by a date field (date1), but
I am struggling with an idea I want put into a python script. I'm
I want to put some numbers from Excel to array. Let's say I have
I have the following option text that I want to put into a variable
I am new to Python. I want to put the result of a SQL
how do i sort the result( list ) returned byhibernate dynamically.Means i dont want
I want to put random output from my result set (about 1.5 mil rows)
I've projected an Intranet Ajax application and I want put it in a full
hey, hi i want put limit on object creation means a class can have

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.