Simple sort with header

Hi,
Please help with this problem. Somehow does not work for me.

test.txt
 CHR          SNP         BP   A1      C_A      C_U   A2        CHISQ            P           OR
  19   rs10401969   19268718    C      222      890    T      0.03462       0.8524       0.9857
   1   rs10873889   76734548    G      934     3811    A       0.5325       0.4656       0.9691
   1   rs11589059  214196749    C      271     1084    T      0.01928       0.8896       0.9902

I want to sort on Col CHISQ ($8) keeping the header intact.
I used this code from another thread

gawk 'NR==1; {if(NR > 1) {print $0 | "sort -t '\t' -nk 8,8"}}' test.txt

However it does not work for me. :confused:
Thanks

{
 IFS= read -r line
 printf "%s\n" "$line"
 sort -t '\t' -nk 8,8 
} < test.txt
1 Like

If your file isn't tab delimited then:

$
$
$ cat f2
 CHR          SNP         BP   A1      C_A      C_U   A2        CHISQ            P           OR
  19   rs10401969   19268718    C      222      890    T      0.03462       0.8524       0.9857
   1   rs10873889   76734548    G      934     3811    A       0.5325       0.4656       0.9691
   1   rs11589059  214196749    C      271     1084    T      0.01928       0.8896       0.9902
$
$
$ gawk 'NR==1; {if(NR > 1) {print $0 | "sort -nk 8,8"}}' f2
 CHR          SNP         BP   A1      C_A      C_U   A2        CHISQ            P           OR
   1   rs11589059  214196749    C      271     1084    T      0.01928       0.8896       0.9902
  19   rs10401969   19268718    C      222      890    T      0.03462       0.8524       0.9857
   1   rs10873889   76734548    G      934     3811    A       0.5325       0.4656       0.9691
$
$

tyler_durden

1 Like

Try without -t option in sort

gawk 'NR==1; {if(NR > 1) {print $0 | "sort -nk 8,8"}}' test.txt
1 Like

Thank you all.
That worked fine!