Sort in unix while the other field does not change

I have a file

1 4 2 1 2
1 1 2 4 5
1 2 4 5 9
2 3 4 5 1
1 0 2 1 5
2 2 2 1 1

sort -k1 file gives

1 0 2 1 5
1 1 2 4 5
1 2 4 5 9
1 4 2 1 2
2 2 2 1 1
2 3 4 5 1

I want only the first field to be sorted, others stay where they should be, for example. A sorted file should contain:

1 4 2 1 2
1 1 2 4 5
1 2 4 5 9
1 0 2 1 5
2 3 4 5 1
2 2 2 1 1

Similarly, sorting -k1r testsort gives

2 3 4 5 1
2 2 2 1 1
1 4 2 1 2
1 2 4 5 9
1 1 2 4 5
1 0 2 1 5

When I want him to be

2 3 4 5 1
2 2 2 1 1
1 4 2 1 2
1 1 2 4 5
1 2 4 5 9
1 0 2 1 5

How to do it in unix?

+4
source share
1 answer

try the following:

sort -s -n -k 1,1

This will work, and to find out more, you can see here.

+11
source

Source: https://habr.com/ru/post/1530389/


All Articles