0

I am trying to sort a genetic file based on chr (column 2) and position (column 3) that both start from lower to higher. my table is like this in large file

SNP CHR BP  A1  A2  effect_allele_frequency BETA    standard_error  P
rs10875231  1   100000012   T   G   0.405   -0.0456807  0.02260471  0.04335677
rs6678176   1   100000827   C   T   0.383   0.02553138  0.02287662  0.2645817
rs78590530  1   100000948   A   G   0.016   0.171376    0.08757958  0.05035017
rs149636485 1   100001060   A   G   0.004   -0.03363731 0.1819208   0.8529224

I want to order CHR (from 1 to 22) in a way that position also start from lower to high disregard of other columns and for each chr separately. I tried this sort command

sort -t $'\t' -nk3 myfile.tsv | sort -t $'\t' -nk2  > test.txt

it gives order in chr (column 2) but not position (column 3). it seems that column 1 interferes:

SNP CHR BP  A1  A2  effect_allele_frequency BETA    standard_error  P
rs1000033   1   226580387   G   T   0.416   0.02958699  0.02295015  0.1971771
rs1000050   1   162736463   T   C   0.378   0.06136397  0.02293639  0.007468015
rs1000070   1   222359612   C   T   0.381   0.02563547  0.02294139  0.2638107
rs1000073   1   157255396   G   A   0.387   -0.01470793 0.02273634  0.517414
rs1000085   1   66857915    C   G   0.024   -0.03536382 0.07555889  0.6394446
rs1000127   1   63432716    C   T   0.157   0.003052272 0.03045933  0.919875

How can I sort by column 2 then 3 only?

1
  • You only have numerical chromosomes? Commented May 11, 2023 at 6:05

1 Answer 1

2

You need to specify all keys in sort order in a single sort invocation, and you need to give end fields:

sort -n -k2,2 -k3,3

sort -n -k3 means sort by comparing numbers formed by taking numeric characters starting with field 3 (after skipping blanks). Piping to a separate sort -n -k2 sorts by the contents of the second and following fields (until a non-numeric character). But that doesn’t explain your output...

See the description of -n and -k in the sort specification for details.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.