0

Supposing I have a file F:

hi xb 9 test
hi xc 8 test
hi xa 7 test

I need to sort the lines of this file. But I have two ways I want to sort them.

Method 1: Sort by column. How do I sort the lines by lets say column 2 (xb, xc, xa) or say column 3 (8, 9, 7)?

Expected output if sorted by column 2:

hi xa 7 test
hi xb 9 test
hi xc 8 test

Method 2: Sort by the number on column 3. What is the regex I need to capture the number on column 3?

Expected output:

hi xa 7 test
hi xc 8 test
hi xb 9 test
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Varun Patro
  • 2,059
  • 3
  • 14
  • 17
  • 2
    Please ask just one question at the time. Also, it is not clear what you want in Method 1. Add a desired output as well. Method 2 is easy, just do some research and I am sure you'll find tons of examples. – fedorqui Jun 15 '16 at 10:57
  • I thought it would be wasteful to ask two questions. So combined them into one. They are also both quite similar. – Varun Patro Jun 15 '16 at 11:01
  • 4
    `sort -k2,2 file` and `sort -nk3,3 file` – anubhava Jun 15 '16 at 11:01

0 Answers0