<p>The fields aren't only the genotype. They contain much more information. So <code>$9</code> will never be exactly <code>1|1</code>, what you are interested in is that the field <em>starts with</em> <code>1|1</code>. Also, if you need all of the conditions to match (meaning that at least one of the criteria for each sample is met), you need <code>&amp;&amp;</code> not <code>||</code>. So try this instead:</p>

<pre><code>awk '(<span class="math-container">$6~/^1\/0/   || $</span>6~/^0\/1/ || <span class="math-container">$6~/^1\\|0/   || $</span>6~/^0\\|1/) &amp;&amp;
     (<span class="math-container">$7~/^1\/0/   || $</span>7~/^0\/1/ || <span class="math-container">$7~/^1\\|0/   || $</span>7~/^0\\|1/  ) &amp;&amp;
     (<span class="math-container">$8~/^1\/0/   || $</span>8~/^0\/1/ || <span class="math-container">$8~/^1\\|0/   || $</span>8~/^0\\|1/) &amp;&amp;
     (<span class="math-container">$9~/^1\\|1/  || $</span>9~/^1\/1/ ) &amp;&amp;
     (<span class="math-container">$10~/^0\\|0/ || $</span>10~/^0\/0/ ) &amp;&amp;
     (<span class="math-container">$11~/^1\/0/  || $</span>11~/^0\/1/ || <span class="math-container">$11~/^1\\|0/ || $</span>11~/^0\\|1/) &amp;&amp;
     (<span class="math-container">$12~/^1\\|1/ || $</span>12 ~/^1\/1/)' foo.vcf
</code></pre>

<p>Running that on your file returns:</p>

<pre><code>1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0
</code></pre>

CC BY-SA 4.0
2019-10-09T11:17:54.847Z
Hi @terdon, is there a particular reason for escaping the `|` with two backslahes?
haci
CC BY-SA 4.0
2019-10-09T11:29:12.393Z
@haci yes, the double escape is needed inside `//` in `awk`. Something about how awk interprets regexes in its match (`//`) operator. An unescaped `|` means "OR" in regular expressions, so it needs to be escaped. The double escape is an awk thing.
terdon
CC BY-SA 4.0
2019-10-09T11:53:54.060Z
Could you please take a look at this question? https://bioinformatics.stackexchange.com/questions/10548/awk-find-pattern The answer I proposed for that question (which basically is more or less your answer above except for single backslashes to escape `|`) gives the expected output, whereas the one with double backslashes matches lines/columns it should not and I cannot figure out why.
haci
CC BY-SA 4.0
2019-10-09T13:05:15.160Z
@haci um. I don't know. I can't figure it out either. I know `awk` sometimes requires the double escaping, but I never remember exactly when or why. I also know that in this case, it doesn't work without the double escaping, but you're absolutely right, in your answer it fails _with_ the double. I think I'll probably have to post a question on [linux.se], I just need to make a minimal reproducible example of this. I'll let you know, if I do.
terdon
CC BY-SA 4.0
2019-09-29T11:55:09.487Z
2019-10-09T12:59:49.617Z
2019-10-09T12:59:49.617Z
terdon
<p>I have a file where i what to check for a pattern below and print out all the lines that matches the pattern but the output is empty.</p>

<p>here is the pattern that i am looking for.</p>

<ul>
<li>Father = 1/0 or 0/1 or 1|0 or 0|1</li>
<li>Mother = 1/0 or 0/1 or 1|0 or 0|1</li>
<li>Daughter 1 = 1|0 or 0|1 or 1/0 or 0/1</li>
<li>Daughter 2 = 1|1 or 1/1</li>
<li>Daughter 3 = 0|0 or 0/0</li>
<li>Son 1 = 1|0 or 0|1 or 1/0 or 0/1</li>
<li>Son 2 = 1|1 or 1/1</li>
</ul>

<p>mysample file</p>

<pre><code>#CHROM POS ID REF ALT FATHER MOTHER DAUGHTER1 DAUGHTER2 DAUGHTER3 SON1 SON2
1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0
</code></pre>

<p>I tried this, but the output is empty:</p>

<pre><code>awk  '<span class="math-container">$9=="1|1"  || $</span>10=="0|0" || <span class="math-container">$12=="1|1" || $</span>8=="1|0" || <span class="math-container">$11=="1|0" || $</span>6=="1/0"||$7 =="1/0" {print}' variants.vcf &gt; test.txt
</code></pre>

CC BY-SA 4.0
2019-09-29T11:33:12.923Z
Should it match all conditions or any conditions? If Daughter 2 = 1|1 or 1/1, should that line be printed irrespective of the other genotypes?
terdon
CC BY-SA 4.0
2019-09-29T11:46:12.620Z
Yes should match all
user11766958
CC BY-SA 4.0
2019-09-29T11:48:08.437Z
So, print all lines where at least one of each of your bullet points is true? Then why are you using `||` in your `awk`?
terdon
<awk>
awk find Sickle cell anemia from father, mother, son, daughter
# awk find Sickle cell anemia from father, mother, son, daughter

(Asked by: username_0 on 2019-09-29)

I have a file where i what to check for a pattern below and print out all the lines that matches the pattern but the output is empty.
here is the pattern that i am looking for.

Father = 1/0 or 0/1 or 1|0 or 0|1
Mother = 1/0 or 0/1 or 1|0 or 0|1
Daughter 1 = 1|0 or 0|1 or 1/0 or 0/1
Daughter 2 = 1|1 or 1/1
Daughter 3 = 0|0 or 0/0
Son 1 = 1|0 or 0|1 or 1/0 or 0/1
Son 2 = 1|1 or 1/1

mysample file
#CHROM POS ID REF ALT FATHER MOTHER DAUGHTER1 DAUGHTER2 DAUGHTER3 SON1 SON2
1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0

I tried this, but the output is empty:
awk  '$9=="1|1"  || $10=="0|0" || $12=="1|1" || $8=="1|0" || $11=="1|0" || $6=="1/0"||$7 =="1/0" {print}' variants.vcf > test.txt

**Tags:** awk
> **Comments:**
> **username_1**: Should it match all conditions or any conditions? If Daughter 2 = 1|1 or 1/1, should that line be printed irrespective of the other genotypes?  
> **username_0**: Yes should match all  
> **username_1**: So, print all lines where at least one of each of your bullet points is true? Then why are you using `||` in your `awk`?  

---

## 1 answers

### Answer by username_1 on 2019-09-29. Score: 1 | Accepted answer

The fields aren't only the genotype. They contain much more information. So $9 will never be exactly 1|1, what you are interested in is that the field starts with 1|1. Also, if you need all of the conditions to match (meaning that at least one of the criteria for each sample is met), you need && not ||. So try this instead:
awk '($6~/^1\/0/   || $6~/^0\/1/ || $6~/^1\\|0/   || $6~/^0\\|1/) &&
     ($7~/^1\/0/   || $7~/^0\/1/ || $7~/^1\\|0/   || $7~/^0\\|1/  ) &&
     ($8~/^1\/0/   || $8~/^0\/1/ || $8~/^1\\|0/   || $8~/^0\\|1/) &&
     ($9~/^1\\|1/  || $9~/^1\/1/ ) &&
     ($10~/^0\\|0/ || $10~/^0\/0/ ) &&
     ($11~/^1\/0/  || $11~/^0\/1/ || $11~/^1\\|0/ || $11~/^0\\|1/) &&
     ($12~/^1\\|1/ || $12 ~/^1\/1/)' foo.vcf

Running that on your file returns:
1 736689 . T A 0/0:37:1.224:2.367:0.054:0.0:108:0.00:4.75:0.53:0.00:T,37,2.367:37,0 1/0:33:1.099:3.320:0.091:358.5:359:0.59:4.34:0.59:0.08:A,15,1.149,T,18,2.171:18,15 0|0:30:0.999:1.314:0.033:0.0:87:0.00:1.16:0.00:0.00:T,30,1.314:30,0 0|1:31:1.032:2.299:0.065:211.5:212:2.61:0.48:0.07:0.43:A,12,0.124,C,1,0.010,T,18,2.165:18,12 0|1:37:1.232:2.374:0.054:263.2:263:1.47:8.69:0.06:0.05:A,16,1.160,T,21,1.214:21,16 0|0:43:1.423:2.447:0.047:0.0:98:0.20:0.83:0.45:0.00:A,1,0.010,T,42,2.437:42,1 0|1:40:1.323:3.393:0.075:280.5:281:1.95:2.36:3.47:0.27:A,17,1.175,T,23,2.219:23,17
1 758663 . CGCC TTAG 0/0:27:0.893:0.296:0.000:0.0:90:5.15:0.85:3.67:0.01:27,0 0/1:21:0.699:0.234:0.000:2333.1:353:0.93:2.17:2.36:0.00:9,12 0|0:35:1.166:0.370:0.000:0.0:108:3.97:3.43:4.75:0.00:34,0 0|1:33:1.099:0.375:0.000:2820.8:447:0.07:8.69:0.53:0.00:17,15 0|1:23:0.766:0.232:0.000:1860.0:382:0.85:0.17:0.09:0.00:13,10 0|0:23:0.761:0.255:0.000:0.0:75:1.51:2.59:0.78:0.00:22,0 0|1:18:0.596:0.215:0.000:1686.8:275:0.00:2.17:0.10:0.00:9,9
1 762005 . G C 1/0:36:1.191:5.321:0.139:360.9:357:0.00:1.93:0.97:0.00:C,18,2.166,G,18,3.156:18,18 0/0:24:0.799:4.207:0.167:0.0:60:0.00:0.36:3.26:0.00:G,24,4.207:24,0 1|0:40:1.332:3.384:0.075:332.2:332:0.22:13.83:0.22:0.00:C,19,1.186,G,21,2.197:21,19 1|0:31:1.032:3.290:0.097:236.2:236:1.75:8.18:0.07:0.00:C,13,0.134,G,18,3.156:18,13 1|0:32:1.066:4.290:0.125:187.0:187:5.67:1.78:9.77:0.00:C,11,0.114,G,20,3.177,T,1,1.000:20,11 1|0:43:1.423:4.405:0.093:181.6:182:10.13:1.24:0.45:0.00:C,14,2.124,G,28,2.270,T,1,0.010:28,14 0|0:34:1.125:2.332:0.059:0.0:96:0.00:4.09:0.26:0.00:G,34,2.332:34,0

> **Comments:**
> **username_2**: Hi @terdon, is there a particular reason for escaping the `|` with two backslahes?  
> **username_1**: @haci yes, the double escape is needed inside `//` in `awk`. Something about how awk interprets regexes in its match (`//`) operator. An unescaped `|` means "OR" in regular expressions, so it needs to be escaped. The double escape is an awk thing.  
> **username_2**: Could you please take a look at this question? https://bioinformatics.stackexchange.com/questions/10548/awk-find-pattern The answer I proposed for that question (which basically is more or less your answer above except for single backslashes to escape `|`) gives the expected output, whereas the one with double backslashes matches lines/columns it should not and I cannot figure out why.  
> **username_1**: @haci um. I don't know. I can't figure it out either. I know `awk` sometimes requires the double escaping, but I never remember exactly when or why. I also know that in this case, it doesn't work without the double escaping, but you're absolutely right, in your answer it fails _with_ the double. I think I'll probably have to post a question on [linux.se], I just need to make a minimal reproducible example of this. I'll let you know, if I do.  

---

