Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I sort an array

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
I have the following Korn shell variable which I need to output in sorted order (by field 1 with delimiter ,)

# Original list = "4,Test1,a,b 5,Test2,g,o 2,Test6,a,q
1,Test2,a,c"
# Required list = "1,Test2,a,c 2,Test6,a,q 4,Test1,a,b
5,Test2,g,o"

IN="4,Test1,a,b 5,Test2,g,o 2,Test6,a,q
1,Test2,a,c"

OUT=`echo $IN | sort -k1`
echo $OUT

How do I sort this list ?
 
Try:

#!/bin/ksh
# Original list = "4,Test1,a,b 5,Test2,g,o 2,Test6,a,q 1,Test2,a,c"
# Required list = "1,Test2,a,c 2,Test6,a,q 4,Test1,a,b 5,Test2,g,o"

IN="4,Test1,a,b 5,Test2,g,o 2,Test6,a,q 1,Test2,a,c"

rm /tmp/field >/dev/null
for field in $IN
do
echo $field >> /tmp/field
done
sort /tmp/field > /tmp/field2
cat /tmp/field2 | tr "\n" " " "Long live king Moshiach !"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top