awk to print out with two possibilities


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print out with two possibilities
# 1  
Old 07-12-2017
awk to print out with two possibilities

The below awk adds a header Variants Detected: followed by, in this case the line in
file underneath the ## (like the desired result). The script does execute and works as expected
(looks for the keywords in file and prints if found underneath header) if I remove the last awk, which
prints the message if the output is blank. With the last awk the blank output always prints.
I can not seem to script the awk correctly to account for both possibilities. There are typically multiple lines
in the file if it is not black/empty. Thank you Smilie.

file
Code:
##fileformat=VCFv4.1
##fileDate=20170422
chr17    7577108    COSM10749;COSM43737    C    A,T    149.594    PASS    AF=0.0830415,0.0;AO=372,2;DP=4420;FAO=166,0;FDP=1999;FR=.,.,REALIGNEDx0.0865;FRO=1833;FSAF=82,0;FSAR=84,0;FSRF=952;FSRR=881;FWDB=0.0072184,-0.0207142;FXX=4.99998E-4;HRUN=1,1;LEN=1,1;MLLD=293.795,80.5366;OALT=A,T;OID=COSM10749,COSM43737;OMAPALT=A,T;OPOS=7577108,7577108;OREF=C,C;PB=.,.;PBP=.,.;QD=0.299338;RBI=0.00721997,0.02565;REFB=1.40155E-4,-7.81395E-4;REVB=1.50579E-4,0.0151276;RO=4043;SAF=187,1;SAR=185,1;SRF=2118;SRR=1925;SSEN=0,0;SSEP=0,0;SSSB=-0.0251826,-5.12306E-4;STB=0.52327,0.5;STBP=0.541,1.0;TYPE=snp,snp;VARB=-0.00153404,0.0;HS;FUNC=[{'origPos':'7577108','origRef':'C','normalizedRef':'C','gene':'TP53','normalizedPos':'7577108','normalizedAlt':'A','polyphen':'1.0','gt':'pos','codon':'TTT','coding':'c.830G>T','sift':'0.0','grantham':'205.0','transcript':'NM_000546.5','function':'missense','protein':'p.Cys277Phe','location':'exonic','origAlt':'A','exon':'8','oncomineGeneClass':'Loss-of-Function','oncomineVariantClass':'Hotspot'}]    GT:GQ:DP:FDP:RO:FRO:AO:FAO:AF:SAR:SAF:SRF:SRR:FSAR:FSAF:FSRF:FSRR:QT    0/1:149:4420:1999:4043:1833:372,2:166,0:0.0830415,0.0:185,1:187,1:2118:1925:84,0:82,0:952:881:1

awk
Code:
printf "Variants Detected: \n" >> out | awk NR>2 -v p1="PASS" -v p2="'oncomineGeneClass'" -v p3="'oncomineVariantClass':" '$0 ~ p1 && $0 ~ p2 && $0 ~ p3' file | awk 'BEGIN{if(p1=="" && p2=="" && p3==""){print "nothing detected"}}'>> out

desired output
Code:
Variants Detected:
chr17    7577108    COSM10749;COSM43737    C    A,T    149.594    PASS    AF=0.0830415,0.0;AO=372,2;DP=4420;FAO=166,0;FDP=1999;FR=.,.,REALIGNEDx0.0865;FRO=1833;FSAF=82,0;FSAR=84,0;FSRF=952;FSRR=881;FWDB=0.0072184,-0.0207142;FXX=4.99998E-4;HRUN=1,1;LEN=1,1;MLLD=293.795,80.5366;OALT=A,T;OID=COSM10749,COSM43737;OMAPALT=A,T;OPOS=7577108,7577108;OREF=C,C;PB=.,.;PBP=.,.;QD=0.299338;RBI=0.00721997,0.02565;REFB=1.40155E-4,-7.81395E-4;REVB=1.50579E-4,0.0151276;RO=4043;SAF=187,1;SAR=185,1;SRF=2118;SRR=1925;SSEN=0,0;SSEP=0,0;SSSB=-0.0251826,-5.12306E-4;STB=0.52327,0.5;STBP=0.541,1.0;TYPE=snp,snp;VARB=-0.00153404,0.0;HS;FUNC=[{'origPos':'7577108','origRef':'C','normalizedRef':'C','gene':'TP53','normalizedPos':'7577108','normalizedAlt':'A','polyphen':'1.0','gt':'pos','codon':'TTT','coding':'c.830G>T','sift':'0.0','grantham':'205.0','transcript':'NM_000546.5','function':'missense','protein':'p.Cys277Phe','location':'exonic','origAlt':'A','exon':'8','oncomineGeneClass':'Loss-of-Function','oncomineVariantClass':'Hotspot'}]    GT:GQ:DP:FDP:RO:FRO:AO:FAO:AF:SAR:SAF:SRF:SRR:FSAR:FSAF:FSRF:FSRR:QT    0/1:149:4420:1999:4043:1833:372,2:166,0:0.0830415,0.0:185,1:187,1:2118:1925:84,0:82,0:952:881:1

desired output if blank
Code:
Variants Detected:
nothing detected


Last edited by cmccabe; 07-12-2017 at 12:21 PM.. Reason: fixed format
# 2  
Old 07-12-2017
Try:
Code:
awk -v p1="PASS" -v p2="'oncomineGeneClass'" -v p3="'oncomineVariantClass':" '
  !/^#/ { 
    printf "Variants Detected: \n%s\n",($0 ~ p1 && $0 ~ p2 && $0 ~ p3) ? $0 : "nothing detected"
  }
' file > file.out

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-12-2017
This one prints the "Variants Detected:" only once, before the first match. And at the END it prints "nothing detected" if there was not a single match. It uses a control variable wasfound.
Code:
awk -v p1="PASS" -v p2="'oncomineGeneClass'" -v p3="'oncomineVariantClass':" '
  !/^#/ && $0 ~ p1 && $0 ~ p2 && $0 ~ p3 {
    if (!wasfound) {
      print "Variants Detected:"
      wasfound=1
    }
    print
  }
  END {
    if (!wasfound) { print "nothing detected" }
  }
' file

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-13-2017
Thank you both Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find the available number possibilities

Hi, I try to get the combination of 6 numbers from (1-60) using any script. Any script that can help me to fix this ... EG: The output should be like that different possibilities from 1-60... 1) 1 3 15 29 30 42 2) 4 5 6 31 44 60 like the above ... Hereas, I need all the possibilities... (5 Replies)
Discussion started by: gsiva
5 Replies

2. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

configure bash completion for multiple possibilities

Hello, Bash completion is great, but there are some features I'd like to change. One thing is the behaviour when there are lots of very similar possibilities. E.g., my directory contains 133 files, from pubmed_result1.txt to pubmed_result133.txt $ ls Lyonprim/p Display all 133... (2 Replies)
Discussion started by: jossojjos
2 Replies

4. Ubuntu

Restricted access possibilities

Hi, I have given a laptop from company with Ubuntu 10.04 on it. I have restricted access over it, means I have been given sudo login on it. SO I am unable to so many major activities over it, Can you all people tell me the Terminal tricks that I can use to get my hands on it. (1 Reply)
Discussion started by: nixhead
1 Replies

5. Shell Programming and Scripting

Find command possibilities

Using find command is the below possible ? find /home/abcd/****/efgh where the "****" can be any name/character. Thanks, Vasanth. (2 Replies)
Discussion started by: skcvasanth
2 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies

8. UNIX for Dummies Questions & Answers

What can you do with unix what are the possibilities

Where do i get unix at (1 Reply)
Discussion started by: shinobikil
1 Replies
Login or Register to Ask a Question