0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

My solution still fits on your desired output. Can you elaborate a little more what is wrong?
@user2207811 try sort -k1.2,1.2 your_file
OK, now I see the difference. As blah would be something different, a1 blah would be after b1 alah with my answer. Thanks @ravoori. Anyway, user2207811, if your input is not representative and the question does not explicitly state the problem then don't be surprised when an answer doesn't not work for cases that you failed to mention.
OK, thanks. We all learnt something: me the sort -k1.2,1.2 issue. You to indicate proper input and output. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.