Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk to print out with two possibilities

Shell Programming and Scripting


Tags
awk, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-12-2017   -   Original Discussion by cmccabe
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 17 November 2017, 8:12 AM EST
Location: Chicago
Posts: 1,188
Thanks: 713
Thanked 14 Times in 13 Posts
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 Linux.

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
Sponsored Links
    #2  
Old Unix and Linux 07-12-2017   -   Original Discussion by cmccabe
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 November 2017, 10:59 AM EST
Location: Amsterdam
Posts: 11,620
Thanks: 517
Thanked 3,380 Times in 2,979 Posts
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

The Following User Says Thank You to Scrutinizer For This Useful Post:
cmccabe (07-13-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-12-2017   -   Original Discussion by cmccabe
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 19 November 2017, 10:56 AM EST
Location: Simplicity
Posts: 3,823
Thanks: 318
Thanked 1,281 Times in 1,159 Posts
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

The Following User Says Thank You to MadeInGermany For This Useful Post:
cmccabe (07-13-2017)
    #4  
Old Unix and Linux 07-13-2017   -   Original Discussion by cmccabe
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 17 November 2017, 8:12 AM EST
Location: Chicago
Posts: 1,188
Thanks: 713
Thanked 14 Times in 13 Posts
Thank you both Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Restricted access possibilities nixhead Ubuntu 1 08-02-2011 06:16 AM
Find command possibilities skcvasanth Shell Programming and Scripting 2 12-04-2010 03:15 AM
What can you do with unix what are the possibilities shinobikil UNIX for Dummies Questions & Answers 1 07-30-2004 06:21 AM



All times are GMT -4. The time now is 12:11 PM.