Farah Bayomi
Farah Bayomi

Reputation: 11

Sorting an array and saving its output in variable

I have an array

array = (Testcase_5_Input_Packets Testcase_3_Input_Packets
 Testcase_1_Input_Packets Testcase_4_Input_Packets Testcase_2_Input_Packets)

i want to sort its elements and save its sorted contents in an array to be like:

array = Testcase_1_Input_Packets
        Testcase_2_Input_Packets
        Testcase_3_Input_Packets
        Testcase_4_Input_Packets
        Testcase_5_Input_Packets

How do i do that in bash ?

Upvotes: 0

Views: 119

Answers (2)

Jack Simth
Jack Simth

Reputation: 151

IF your data contains no newlines, then this is straightforward:

rawdata=("Data 1" "Data 3" "Data 2" "Data 4")
tmpfile=/dev/shm/tmp.$$
touch "$tmpfile"
chmod 600 "$tmpfile"
for e in "${rawdata[@]}" 
do
    echo "$e" >> "$tmpfile"
done
sortdata=$(cat "$tmpfile" | sort)
echo "$sortdata" > "$tmpfile"
sortedarray=()
while read line
do
    sortedarray+=("$line")
done < "$tmpfile"
rm -f "$tmpfile"

BUT: This will break if an element includes a newline, as that's what sort uses as a delimeter. This is solvable, but you'd need to ID a string that isn't used in your data, trade in-element newlines for that as you write them to the file, and trade them back as you read them out. Once you've ID'd the string, that would look something like:

rawdata=("Data 1" "Data 3" "Data 2" "Data 4")
tmpfile=/dev/shm/tmp.$$
touch "$tmpfile"
chmod 600 "$tmpfile"
for element in "${rawdata[@]}" 
do
    alteration=$(echo "$element" | sed 's/\n/NEWLINEMARKER/g')
    alteration=$(echo "$alteration" | sed 's/NEWLINEMARKER$//g')
    echo "$alteration" >> "$tmpfile"
done
sortdata=$(cat "$tmpfile" | sort)
echo "$sortdata" > "$tmpfile"
sortedarray=()
while read line
do
    element=$(echo "$line" | sed 's/NEWLINEMARKER/\n/g')
    sortedarray+=("$element")
done < "$tmpfile"
rm -f "$tmpfile"

Good Coding!

Upvotes: -1

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

If array elements don't contain newline characters, then this one-liner should do the trick:

readarray -t sorted_array < <(printf '%s\n' "${array[@]}" | sort)

Upvotes: 2

Related Questions