awk to print lines that meet conditions and have value in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print lines that meet conditions and have value in another file
# 1  
Old 01-04-2018
awk to print lines that meet conditions and have value in another file

I am trying to use awk to print lines that satisfy either of the two conditions below:

condition 1: $2 equals CNV and the split of $3, the value in red, is greater than or equal to 4. ---- this is a[1] or so I think
condition 2: $2 equals CNV and the split of $3, the value in red --- this is a[1] or so I think, is less than or equal to 1.0 and the value in green --- this is a[3] or so I thnk in less than or equal to 1.9 and $4 matches a line in list. I have added comments to the code as to what I think is happening. The code execcutes but all the CNV lines are printed currently. Thank you Smilie.

file
Code:
chr1:11184539	CNV	5%:5.5,95%:2.68	Name
chr1:11184539	REF
chr1:11184539	SNV		A
chr1:11184555	CNV	5%:0.9,95%:1.9	BRCA1
chr1:11184539	FUSION
chr1:11184539	INDEL	G
chr1:11184555	CNV	5%:2.5,95%:2.68	Name2
chr1:11184555	CNV	5%:1.1,95%:1.8	BRCA2

list
Code:
BRCA1
BRCA2

awk
Code:
awk -F'\t' '{split($3,a,":,")} $2=="CNV" && a[1]>=4.0' file    # capture condition 1  --- spilt $3 on : and , and check if $2 is CNV, compare a[1] >=4.0 ----
awk -F'\t' '{split($3,a,":,")} $2=="CNV" && a[1]<=1.0 && a[3]<=1.9 && NR==FNR{c[$1]++;next};c[$1] > 0' file list  # capture condition 2 --- spilt $3 on : and , and check if $2 is CNV, compare a[1] ,=1.0 and a[3] <=1.9 and $4 is matches $1 in list ----

desired output
Code:
chr1:11184539	CNV	5%:5.5,95%:2.68	Name
chr1:11184555	CNV	5%:0.9,95%:1.9	BRCA1

# 2  
Old 01-04-2018
Code:
awk '
NR==FNR {a[$1]=$1; next;}
$2=="CNV" {
   c=split($3, b, "[,:]");
   if (b[2]>=4.0 || (b[2]<=1.0 && b[c]<=1.9 && length(a[$NF]))) print $0;
}
' list FS="\t" infile


Last edited by rdrtx1; 01-04-2018 at 05:25 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 01-08-2018
Thank you very much for your help Smilie.
# 4  
Old 01-08-2018
You do not need to store the key here; saves some memory:
Code:
awk '
NR==FNR { a[$1]; next }
$2=="CNV" {
  c=split($3, b, "[,:]")
  if (b[2]>=4.0 || (b[2]<=1.0 && b[c]<=1.9 && ($NF in a))) print
}
' list FS="\t" file

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-09-2018
Thank you very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print header and lines that meet both conditions in awk

In the awk below I am trying to print only the header lines starting with # or ## and the lines that $7 is PASS and AF= is less than 5%. The awk does execute but returns an empty file and I am not sure what I am doing wrong. Thank you. file ... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to capture lines that meet either condition

I am trying to modify and understand an awk written by @Scrutinizer The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI=,0.95: portion in blue in file is <1.9. The... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to print out lines that do not fall between range in file

In the awk below I am trying to print out those lines in file2 that are no between $2 and $3 in file1. Both files are tab-delimeted and I think it's close but currently it is printeing out the matches. The --- are not part of the files they are just to show what lines match or fall into the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

awk to print matching lines in files that meet critera

In the tab delimited files below I am trying to match $2 in file1 to $2 of file2. If a match is found the awk checks $3 of file2 and if it is greater than 40% and $4 of file2 is greater than 49, the line in file1 is printed. In the desired output line3 of file1 is not printed because $3 off file2... (9 Replies)
Discussion started by: cmccabe
9 Replies

6. UNIX for Dummies Questions & Answers

Print lines meet requirement

Dear Masters, I have 2 files input below file1 8269229289|CROATIA|LUX 8269229412|ASIA|LUX 8269229371|EUROPE|LUX 8269229355|LANE|LUX 8269229469|SWISS|LUX 8269229477|HAMBURG|LUX 8269229484|EGYPT|LUX 8269229485|GERMANY|LUX 8269229498|CROATIA|LUX File2 8269229289|1100100020... (6 Replies)
Discussion started by: radius
6 Replies

7. Shell Programming and Scripting

grep/awk to only print lines with two columns in a file

Hey, Need some help for command to print only lines with two columns in a file abc 111 cde 222 fgh ijk 2 klm 12 23 nop want the ouput to be abc 111 cde 222 ijk 2 Thanks a lot in advance!!! (3 Replies)
Discussion started by: leo.maveriick
3 Replies

8. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

10. Shell Programming and Scripting

Perl - if conditions is meet, push the last field of $_ into an array

I am using a seed file shown below to separate cisco devices by ios/os type. I want to bunch all the devices based on ios/os version. Once I find a match, I only want to push the ip address into the appropriate array. Example of seedfile 8 host1 (C3500XL-C3H2S-M) 11.0(5)WC17 10.1.44.21 9... (1 Reply)
Discussion started by: popeye
1 Replies
Login or Register to Ask a Question