I have a line (or many lines) of numbers that are delimited by an arbitrary character. What UNIX tools can I use to sort each line's items numerically, retaining the delimiter?
Examples include:
- list of numbers; input:
10 50 23 42
; sorted:10 23 42 50
- IP address; input:
10.1.200.42
; sorted:1.10.42.200
- CSV; input:
1,100,330,42
; sorted:1,42,100,330
- pipe-delimited; input:
400|500|404
; sorted:400|404|500
Since the delimiter is arbitrary, feel free to provide (or extend) an Answer using a single-character delimiter of your choosing.
Best Answer
With
gawk
(GNUawk
) for theasort()
function:replace
*
as the field separator inSEP='*'
with your delimiter.You can also do with the following command in case of a single line (because it's better leave it alone of using shell-loops for text-processing purposes)
replace dots
.
with your delimiter.add
-u
to thesort
command above to remove the duplicates.Notes:
You may need to use
-g, --general-numeric-sort
option ofsort
instead of-n, --numeric-sort
to handle any class of numbers (integer, float, scientific, Hexadecimal, etc).In
awk
no need change, it still will handling those.