I am sorting files which have gene names and their expression values. All files have same exact number of rows ,however after sorting there is a difference in the positioning of certain genes. This is very weird.below are the sorted versions of two such files.

for example:

Cxx1c   25.1695
Cxxc1   15.2228
Cxxc4   0.952061
Cxxc5   3.13309
**Cyb5  157.426**
Cyb561  0.425933
Cyb561a3    9.55082
Cyb561d1    4.00422
Cyb561d2    3.04411
Cyb5b   16.7622
Cyb5d1  7.25191
Cyb5d2  2.85109
Cyb5r1  15.2511
Cyb5r2  0.48748

Another file has this sorting. Basically, in this file Cyb5 is present after Cyb561d2 gene. How can I have exactly same sorting order. Is there any parameter to do such thing?

Cxx1c   44.9795
Cxxc1   19.0346
Cxxc4   1.17429
Cxxc5   2.71589
**Cyb561    7.11003**
Cyb561a3    1.97601
Cyb561d1    2.13004
Cyb561d2    2.03376
Cyb5    64.074
Cyb5b   14.5329
Cyb5d1  12.0212
Cyb5d2  1.47763
Cyb5r1  10.5463
Cyb5r2  0

Here is my code which generates the above sorted file:

for i in *.txt; do
    sort  -d $i >$i.sort
  • What are your separators? Are they all tabs? can you check? – sds Mar 6 '17 at 21:28
  • yes tab separated – Ron Mar 6 '17 at 21:29
  • 2
    sort -d -k1,1 might work – Michael D. Mar 6 '17 at 22:01
  • -n should do it – ctx Mar 6 '17 at 22:05
  • @MichaelD. yes it works.Can you explain the parameters?Also there could be other instances so will that take care of it ? – Ron Mar 6 '17 at 22:09

You are currently sorting on the entire line, but it seems like you only want to sort on the first column. With the way your command is currently written, the columns will basically be concatenated together, eg:

Cyb5    157.426  -> Cyb5157426
Cyb561  0.425933 -> Cyb5610425933


Cyb561  7.11003 -> Cyb561711003
Cyb5    64.074  -> Cyb564074

To sort on just the first column, you'll need to use the following command:

sort -d -k1,1
  • Thanks! what is the difference between -k1 and k1,1 ? – Ron Mar 6 '17 at 22:29
  • field start, stop position. (the stop position defaults to the line's end.) – Michael D. Mar 6 '17 at 22:38

Cyb5 157.426 sorts before Cyb561 0.425933 while Cyb5 64.074 sorts after Cyb561 7.11003 because in your locale, blanks are ignored in the first ordering pass so the order is closer to that of an English dictionary.

For instance, in a dictionary, you'd have a priori in-between apiary and Arrival.

Here, comparing Cyb5 157.426 with Cyb561 0.425933 compares first Cyb5157.426 with Cyb5610.425933 in the first pass because the first weight of the space character is IGNORE.

If you want to avoid that special processing, you can change the locale to C where the order is solely based on the characters code point values.

LC_ALL=C sort -d file

Or as @Swiss said, only sort on the first field:

sort -d -k1,1 file

However note that the field delimiter is the transition from a non-blank to a blank, and the blanks are included in the fields.


 b x
a x

with sort -d -k1,1 would still be sorted as

a x
 b x

in your locale because spaces are ignored, but as:

 b x
a x

In the C locale as the space character sorts before a.

You may want to add the -b option so the leading blanks are always ignored regardless of the locale. Or again, fix the locale to C if you do want lines starting with spaces to be sorted first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.