Tuesday, February 22, 2022

[SOLVED] Sorting space delimited numbers with Linux/Bash

Issue

Is there a Linux utility or a Bash command I can use to sort a space delimited string of numbers?


id='dv4'>

Solution

Here's a simple example to get you going:

echo "81 4 6 12 3 0" | tr " " "\n" | sort -g

tr translates the spaces delimiting the numbers, into carriage returns, because sort uses carriage returns as delimiters (ie it is for sorting lines of text). The -g option tells sort to sort by "general numerical value".

man sort for further details about sort.



Answered By - James Morris
Answer Checked By - David Marino (WPSolving Volunteer)