0

I have the below code, list of string that I'd like to sort.

LIST="APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET"

My goal is to sort it into:

"APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET APP_PATH_10_TARGET"

So I did this:

SORTEDLIST=$(echo ${LIST} | sort -t"_" -k3n,3)

But its still showing:

SORTEDLIST=APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET

I can't find out why the sort doesn't work.

================================================================

Update: This is the code that I'm working on.

I have this ENV variables:

APP_PATH_1_TARGET="/prd/example/1"
APP_PATH_2_TARGET="/prd/example/2"
APP_PATH_3_TARGET="/prd/example/3"
APP_PATH_10_TARGET="/prd/example/4"

The code that doesn't work, because the list is not in expected sequence:

create_app_dir(){
  # Get all variables with name starts with APP_PATH*
  local PARAMLIST=`echo ${!APP_PATH*}`
  echo "PARAMLIST=${PARAMLIST}"
  local SORTEDLIST=$(sort -t_ -k3n <<< ${PARAMLIST// /$'\n'}|tr -s "\n" " ")
  echo "SORTEDLIST=${SORTEDLIST}"

  # Iterate the list and create dir if doesn't exist
  for p in ${SORTEDLIST}; do
    if [[ "${p}" = *_TARGET ]] && [ ! -d "${p}" ]; then

      echo "[+] Creating application directory:${!p}"
      ./make_dir.sh "${!p}"

      if [ $? -ne 0 ]; then
        echo "[-] Error: Unable to create dir." >&2
        return 1
      fi
    fi
  done
}
2
  • What is the actual sequence you are getting in the sortedList? Commented Nov 19, 2018 at 8:23
  • @Abhinandanprasad Exactly this LIST="APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET" Commented Nov 19, 2018 at 8:32

3 Answers 3

3

Because sort is only working with lines by definition. man sort:

sort - sort lines of text files

SORTEDLIST=$(sort -t"_" -k3n,3 <<< ${LIST// /$'\n'}|tr -s "\n" " ")
Sign up to request clarification or add additional context in comments.

3 Comments

I've just tried that and still it doesnt work. I've updated the question with portion of code that I'm working on, with your proposed solution.
@FadhlieIkram Both mine and his solution (they're almost the same) work on my machine with your example code.
That's weird, its not on my machine. Maybe different bash version. Anyways I've managed to make it work, but only after I iterate the list into a temporary file, which I then call for sorting. I'm accepting this answer and thanks for your help :).
0

Try This:

 SORTEDLIST = $(sort -t_ -k3n filename | tr -s '\n\r' ' ')

or

  SORTEDLIST = $(sort -t_ -k3n filename | tr -s '\n' ' ')

Comments

0

sort arranges lines in order. To sort words, you want to re-write each word as a line:

SORTEDLIST=$(printf "%s\n" $LIST | sort -t_ -k3n)

Since you're using Bash, you'd be better using a real list, so that you can get the quoting right:

LIST=(APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET)
SORTEDLIST=($(printf "%s\n" "${LIST[@]}" | sort -t_ -k3n))

for p in "${SORTEDLIST[@]}"

Also, you should avoid using all-uppercase for your shell variables; this convention is used to indicate environment variables intended to change program behaviour.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.