Sort by Column Linux

sort by column linux

sort -V to the rescue:

sort -V file

From man sort:

-V, --version-sort

natural sort of (version) numbers within text


In case you do not have the -V option in your sort command, there is an alternative: sort by first column starting on 4th character (-k1.4) and then sort numerically (-n).

sort -k1.4 -n file

In both cases the output is as follows:

chrX  66.0736
chr1 91.4062
chr2 111.4620
chr3 116.1830
chr4 117.3620
...
chr26 72.2762
chr27 96.2213
chr28 85.5570
chr29 126.3800
chr30 89.5663
chr31 89.1227
chr32 128.6190

how to use Linux command Sort to sort the text file according to 4th column, numeric order?

sort -nk4 file

-n for numerical sort
-k for providing key

or add -r option for reverse sorting

sort -nrk4 file

how can i sort in bash the first column but ignore any other column in order

Use the stable sort:

sort -nsk1,1
  • -n sort numerically
  • -k1,1 sorts by the first column ("from the first to the first")
  • -s means "stable", i.e. keep the input order in case of a draw

Note that not all implementations of sort support the -s, as it's not mentioned in the POSIX specification.

sort numerically based on the first column

sort reads from stdin and command-line as well. Thus if you have a file you can:

sort < file
# or
sort file

if you want sort based on first column you can:

sort -k1 < file

But if fact it does affect the output since by default it does not care about numerical order. Thus you should add -n option:

  -n, --numeric-sort          compare according to string numerical value

and doing it like:

sort -k1 -n < file

it outputs:

061     data1
238 data6
264 data7
439 data9
682 help
1264 moredata
2305 data2
4080 data3
5640 otherdata
9251 data4
11844 data5
33940 data8

and if you provide it with -r it print in reverse order:

33940   data8
11844 data5
9251 data4
5640 otherdata
4080 data3
2305 data2
1264 moredata
682 help
439 data9
264 data7
238 data6
061 data1

Linux sort numerically based on first column

tail -n +2 file.csv | sort -k1,2 -n -t"," should do the trick.

Linux sort by column and in reverse order

I just needed to remove the "n" next to column number:

sort -k 2 -r file.txt

How to sort content of a file in-place by a column?

It's easy if you give up on sorting in place:

sort -k 1 original > by_col_1
sort -k 2 original > by_col_2

Linux shell sort file according to the second column?

If this is UNIX:

sort -k 2 file.txt

You can use multiple -k flags to sort on more than one column. For example, to sort by family name then first name as a tie breaker:

sort -k 2,2 -k 1,1 file.txt

Relevant options from "man sort":

-k, --key=POS1[,POS2]


start a key at POS1, end it at POS2 (origin 1)

POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

-t, --field-separator=SEP


use SEP instead of non-blank to blank transition



Related Topics



Leave a reply



Submit