GuLearn
GuLearn

Reputation: 1864

sort in bash, sort based part of column

I have a file like:

a1 blah
b2 blah
a3 blah
b1 blah
b3 blah
a2 blah

if I do

sort -k1,1 file.name

I'll get this:

a1
a2
a3
b1
b2
b3

However, I want to get this order:

a1
b1
a2
b2
a3
b3

how can I do that? Thanks

Edit: I edited the example, the previous one didn't present the whole problem

Upvotes: 0

Views: 279

Answers (1)

fedorqui
fedorqui

Reputation: 289555

You are looking for sort -kN.M! N.M indicates sort to start from the Mth character on Nth field.

Initial solution:

sort -k1.2 your_file

Updated one:

sort -k1.2,k1.2 your_file

so it will just sort by this specific character and won't go further.

Output:

a1 blah
b1 blah
a2 blah
b2 blah
a3 blah
b3 blah

Upvotes: 2

Related Questions