How can I use the sort command to sort the log file by only the first field in Linux?

I have a log file with the name a.log! I want to sort it by fisrt field! If two lines have the same field, the order will be the original contens'! Content:

 1. 101  c
 2. 100  b
 3. 100  a
 4. 2    d

I expect the result:

 1. 2    d
 2. 100  b
 3. 100  a
 4. 101  c

So, I use this command!

sort -nt 't' -k 1 a.log 

But the result:

 1. 2    d
 2. 100  a
 3. 100  b
 4. 101  c

Many thanks!

+3
source share
2 answers

You forgot to limit key fields. By default, it is used until the end of the line.

sort -nst '\t' -k 1,1 0507.log
+7
source

Look at the page to sort ...

   `-n`, --numeric-sort

      compare according to string numerical value

So here is an example ...

sort -n filename
0
source

All Articles