Using sort command to get numeric ascending order

HI everyone,
I am trying to use the unix sort command to get a list of numbers sorted in ascending order but having trouble in getting it to work.
An example of this issue would be when i am trying to sort the following three
number each on a different line "1" , "2" and "116" the sort command list the
results in the order 1,116,2
i want it to sort it as 1,2,116.
Is there any option in sort command that will help me achieve this?

Regards

-w

Hi.

use -n to sort numerically.

Check the man page.

cat file1
1
116
2

sort -n file1
1
2
116

If you want to sort on a different field, use -k

cat file2
a 1 blah
b 116 even more blah
c 2 more blah

sort -nk2 file2
a 1 blah
c 2 more blah
b 116 even more blah

Thank you very much
I think your reply has solved my issue.
the actual file had a list of Ips that was not getting sorted
by using the -n switch. I had to modify the file to get the
last octet off of the file and use sort -n to get it to work.
Really appreciate your help.

-w

you might want to 'normalize' all the quads of the IP address and sort by the normalized version (assuming all IPs are one per line):

nawk -F. '{for(i=1;i<=NF;i++) printf("%03d", $i); print OFS $0}' myIPfile | sort -n -k1,1 | cut -d ' ' -f2-
1 Like