xyz
xyz

Reputation: 8927

stable sort in linux

I have a file

ksh$ cat test
b d
b c
a b
a a

I want to sort on first field but I want stable sort i.e. order of 2nd field should remain the same. I want output as:

a b
a a
b d
b c

If I try:

ksh$sort -k1 -s test   

I get

a a
a b
b c
b d

Please help,

Thanks

Upvotes: 18

Views: 8627

Answers (2)

KARASZI István
KARASZI István

Reputation: 31467

You must specify the end field:

sort -k1,1 -s test

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

You forgot to constrain the key fields. By default it uses until the end of the line.

sort -k1,1 -s t.txt

Upvotes: 31

Related Questions