0

I have a file like this:

head logistic_results.assoc_3.logistic
 CHR               SNP         BP   A1       TEST    NMISS         OR         STAT            P 
   2   2:129412140:T:C  129412140    C        ADD     1438      1.523         3.89    0.0001004
  15   15:26411414:G:A   26411414    A        ADD     1438     0.5577       -3.889    0.0001005
   7    7:24286442:T:G   24286442    G        ADD     1438     0.7449       -3.889    0.0001007
   7    7:24286638:G:C   24286638    C        ADD     1438     0.7449       -3.889    0.0001007
   2   2:129403636:T:C  129403636    C        ADD     1438      1.741        3.889    0.0001008
  15   15:70363332:A:G   70363332    G        ADD     1438      1.366        3.886     0.000102
   3    3:13698784:G:A   13698784    A        ADD     1438      1.465        3.884    0.0001028
   3    3:32665882:C:A   32665882    A        ADD     1438       1.54        3.883     0.000103
  12   12:32855080:A:G   32855080    G        ADD     1438      4.013        3.883    0.0001031

How do I extract all lines which have 3 in the first column?

I tried this but I got empty file...

grep '^3' logistic_results.assoc_3.logistic > logistic_results.assoc_3.logistic_chr3
awk '/^3/' logistic_results.assoc_3.logistic > logistic_results.assoc_3.logistic_chr3

For this example the result would be this:

  3    3:13698784:G:A   13698784    A        ADD     1438      1.465        3.884    0.0001028
  3    3:32665882:C:A   32665882    A        ADD     1438       1.54        3.883     0.000103
1

2 Answers 2

6

Compare the first non-whitespace field with the string 3:

awk '$1 == "3"' logistic_results.assoc_3.logistic >logistic_results.assoc_3.logistic_chr3

The issue with your commands is that you're expecting the 3 to be the first character on the line, but judging from your sample data, there may be whitespace in front of the number. Using awk with its default field delimiter would place the chromosome name in $1 regardless of leading whitespace characters.

This would also be safer as $1 == "1" would only be true for chromosome 1, whereas a regular expression matching 1 at the start of the field (e.g. with /^[[:blank:]]*1/ or $1 ~ /^1/) would also match e.g. 11 and 12.

1
  • thank you so much for this great explanation!
    – anamaria
    Apr 20, 2020 at 18:50
0

Alternatively:

grep "^[[:blank:]]*3" logistic_results.assoc_3.logistic > logistic_results.assoc_3.logistic_chr3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .