0

I have an array that have the CPU core num and a number for each core. the array is totals.

How can I sort

totals=( CPU0=12345 CPU1=23456 CPU3=01234) 

according to numbers and return the sorted version of cpu number for example (3,0,1) means it is sorted and core 3 is the min and core 1 is the max, in bash? and then assign (3,0,1) to an array?

1
  • Don't you think it would be easier if you just asked us how to write the whole script rather than letting us write it step by step? Commented Jul 23, 2012 at 9:47

1 Answer 1

1

Try this for sorting:

echo ${totals[*]} | tr ' ' '\n' | sort -n -t= -k2

To store only the CPU numbers in a new array, try:

sorted_cpu_numbers=( $(echo ${totals[*]} | tr ' ' '\n' | sort -n -t= -k2 | awk -F= '{print substr($1, 4, length($1))}') )
Sign up to request clarification or add additional context in comments.

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.