2

I have a file with following values.

keyword1 19.02 1.69.94
keyword2 19.01 1.68.118
keyword3 19.02 1.69.119
keyword4 19.01 1.68.115
keyword5 19.02 1.69.304
keyword6 19.02 1.69.39

I want these to be sorted based on 3rd column.

In Linux, the following command using version sort works.

raj@VirtualBox:$ grep 19.02 file.txt | sort -t ' ' -k3 -V
keyword6 19.02 1.69.39
keyword1 19.02 1.69.94
keyword3 19.02 1.69.119
keyword5 19.02 1.69.304

On Unix(Solaris) the version sort seems to be missing. I searched around for working alternative on Solaris, but couldn't find one.

I can also use a solution in python.

1
  • Note that the -V option to sort in your grep 19.02 file.txt | sort -t ' ' -k3 -V example is a non-standard, non-portable GNU extension to the POSIX sort utility. Feb 6, 2019 at 16:03

2 Answers 2

3

You could try reversing the fields and using . as the field separator:

awk '{print $3,$2,$1}' your_file |
   sort -nt. -k1,1 -k2,2 -k3,3 |
   awk '{print $3,$2,$1}'

Also, my solaris system has GNU sort (same as in linux) in /usr/gnu/bin/sort and I don't remember having ever installed it ;-)

0
1

You mentioned python - the following is a python2 solution that will hopefully work in whatever python2 version exists on your Solaris machine

lines = open('file.txt').readlines()
lines = [l.split() for l in lines]
lines = [line for line in lines if line[1] == "19.02"]
def sorter(x):
    z = x[-1].split(".")
    return x[0], x[1], [int(y) for y in z]

lines.sort(key=sorter)
for line in lines:
    print " ".join(line)
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .