Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in unix while other field doesn't change

Tags:

bash

unix

sorting

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 only want to first field to be sorted, others remains where they should be at, e.g. The sorted file should give:

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 sort -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 it 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 can I do this in unix?

like image 656
Mc Kevin Avatar asked Dec 20 '22 17:12

Mc Kevin


1 Answers

try this:

sort -s -n -k 1,1

This will work, and to learn more you can see here

like image 84
Mohammad Kermani Avatar answered Jan 07 '23 06:01

Mohammad Kermani