awk to print matching lines in files that meet critera


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print matching lines in files that meet critera
# 1  
Old 09-14-2016
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 is less than 40%. line4 of file1 is not printed because $4 of file2 is less than 49 Thank you Smilie.

file1
Code:
Chr    Start    Ref    Alt
1    100    A    C
1    1000    -    G
2    200    C    G
2    800    A    -

file2
Code:
Chrom    Position    Var Freq    Qual    Coverage    Ref Cov    Var Cov
1    100    50    100    100    50    50
2    800    50    40    100    50    50
2    200    30    100    50    40    10
1    1000    100    100    100    0    100

awk
Code:
awk -F'\t' -v OFS='\t' 'NR==FNR{A[$2];next}$2 in A
{if($3 >.4 OFS $4 > 49)
print ; next
' file1 file2
awk: cmd. line:2: {if($3 > .4 OFS $4 > 49)
awk: cmd. line:2:                    ^ syntax error
awk: cmd. line:3: } next
awk: cmd. line:3:   ^ syntax error

desired output
Code:
1    100    A    C
1    1000    -    G

# 2  
Old 09-14-2016
Hello cmccabe,

Could you please try following and let me know if this helps you.
Code:
awk 'FNR==NR{A[$2]=$0;next} ($2 in A){printf("%s",((($3 * 100)/$4)>40 && $4>49 && $3 ~ /[[:digit:]]/ && $4 ~ /[[:digit:]]/)?A[$2] ORS:X)}' Input_file1   Input_file2

Output will be as follows.
Code:
1    100    A    C
1    1000    -    G

Thanks,
R. Singh
# 3  
Old 09-14-2016
Try out this one liner...
Code:
awk 'BEGIN{while(getline < "f1" > 0) x[$2]=$0} {printf("%s",($2 in x && $3>40 && $4>49)?x[$2]RS:"")}' f2

# 4  
Old 09-14-2016
Small simplification of shamrock's well working proposal:
Code:
awk 'BEGIN{while(getline < "f1" > 0) x[$2]=$0} $2 in x && $3>40 && $4>49 {print x[$2]}' f2
1	100	A	C
1	1000	-	G

This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-14-2016
Quote:
Originally Posted by shamrock
Try out this one liner...
Code:
awk 'BEGIN{while(getline < "f1" > 0) x[$2]=$0} {printf("%s",($2 in x && $3>40 && $4>49)?x[$2]RS:"")}' f2

That will work on some systems, but the standards don't specify the precedence of file redirection operations and comparison operations. Some versions of awk parse the command:
Code:
getline < "f1" > 0

as:
Code:
getline < ("f1" > 0)

and others parse it as:
Code:
(getline < "f1") > 0

# 6  
Old 09-14-2016
Quote:
Originally Posted by Don Cragun
That will work on some systems, but the standards don't specify the precedence of file redirection operations and comparison operations. Some versions of awk parse the command:
Code:
getline < "f1" > 0

as:
Code:
getline < ("f1" > 0)

and others parse it as:
Code:
(getline < "f1") > 0

The left and right arrow operators have the same precedence but associate left to right so getline < "f1" > 0 has an implied parenthesization of ((getline < "f1") > 0)
I am surprised to know that there are awk(s) out there that do not comply with this rule of precedence and associativity as it predates the the very first and original awk!!!
# 7  
Old 09-14-2016
Quote:
Originally Posted by shamrock
The left and right arrow operators have the same precedence but associate left to right so getline < "f1" > 0 has an implied parenthesization of ((getline < "f1") > 0)
I am surprised to know that there are awk(s) out there that do not comply with this rule of precedence and associativity as it predates the the very first and original awk!!!
What you are saying is true if both "arrow" operators are comparison operators; but it is not true when one of the "arrow" operators is a redirection operator. Historic versions of awk based on AT&T UNIX System utility code do one thing. GNU awk does the other.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 or so I think condition 2: $2 equals CNV and the split of $3, the value in red --- this is a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. 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

3. 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

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. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
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

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

8. Shell Programming and Scripting

Look up between 2 files and print matching lines

Hi, I have 2 large log files in .gz format file 1 contains abcde 12345 23456 . . . . . . . . 09123 (8 Replies)
Discussion started by: aravindj80
8 Replies

9. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

10. 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
Login or Register to Ask a Question