D
D
Dmitry Kuzmin2016-07-29 10:08:11
linux
Dmitry Kuzmin, 2016-07-29 10:08:11

Sorting in a string via sort, how is the delimiter specified?

There is a line:
00000000000000;000022233333;2;NONE;true;100,100,5,1,28;UNKNOWN
You need to sort the numbers 100,100,5,1,28 in descending order.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ky0, 2016-07-29
@ky0

sort does not know how, as far as I remember, it sorts the lines from the very beginning. I recommend considering the option using awk or rearranging the fields (this can be combined).

3
3vi1_0n3, 2017-01-21
@3vi1_0n3

Just like that:

STRING="000000000000000;0000000000022222233333;2;NONE;true;100,100,5,1,28;UNKNOWN"
echo $STRING
NUMBERS=$(echo $STRING | cut -d';' -f 6)
SORTSTRING=${NUMBERS//,/\\n}
SORTED=$( echo $(echo -e $SORTSTRING | sed 's/,/\n/' | sort -n ) | sed 's/ /,/g')
STRING2=${STRING/$NUMBERS/$SORTED}
echo $STRING2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question