Sponsored Content
Top Forums Shell Programming and Scripting awk to match value to a field within +/- value Post 302980724 by cmccabe on Thursday 1st of September 2016 01:39:21 PM
Old 09-01-2016
awk to match value to a field within +/- value

In the awk below I use $2 of filet to search filea for a match. If the values in $2 are exact match this works great. However, that is not always the case, so I need to perform the search using a range of + or - 2. That is if the value in filea $2 is within + or - 2 of filet $2 then it is matched. Thank you Smilie.

filet
Code:
Chrom    Position    Gene Sym
chr11    1776024    CTSD
chr11    6637518    TPP1
chr11    6638506    TPP1

filea
Code:
Index    Start    Gene
115    1776025    CTSD
116    6637518    TPP1
117    6638506    TPP1

awk
Code:
awk 'FNR==1 { next }
>       FNR == NR { file1[$2,$3] = $2 " " $3} # filet search
>       FNR != NR { file2[$2,$3] = $2 " " $3 } # in filea
>       END { print "Match:"; for (k in file1) if (k in file2) print file1[k] # Or file2[k]
>             print "Missing in annovar but found in tvc:"; for (k in file2) if (!(k in file1)) print file2[k]
>             print "Missing in tvc but found in annovar:"; for (k in file1) if (!(k in file2)) print file1[k]
>       }' filea filet > match

current output
Code:
Match:
6637518 TPP1
6638506 TPP1
Missing in filea but found in filet:
1776024 CTSD
Missing in filet but found in filea:
1776025 CTSD

desired output
Code:
Match:
6637518 TPP1
6638506 TPP1
1776024 CTSD --> within 2 margin
Missing in filea but found in filet:
Missing in tvc but found in annovar:

awk trued to get the desired output
Code:
awk 'FNR==1 { next }
>       FNR == NR { file1[$2,$3] = $2 " " $3 } # filea search
>       FNR != NR { file2[$2,$3] = $2 " " $3 } # in filet
>       END { print "Match:"; for (k in file1) if (k in file2) print file1[k] # Or file2[k]
>             print "Missing in filea but found in filet:"; for (k in file2) if (!(k in file1)) print file2[k]
>             print "Missing in filet but found in filea:"; for (k in file1) if (!(k in file2)) print file1[k]
>             if((k-$2)^2<=2^2) {print $0, " --> within 2 margin"; next}
>       }' filea filet > match
awk: cmd. line:7: error: `next' used in END action


Last edited by cmccabe; 09-01-2016 at 02:40 PM.. Reason: fixed format
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how do i pattern match a field with awk?

hi, let's say $numbers = "324 350 587" an so on... what i'm trying to do is this: awk -v numbers="$numbers" '{if (numbers ~ /$2/) print $0, "bla bla"}' file # file looks like this: 214 ..... 215 ... 216 .... 250 ... 324 325 ... 350 something ... ... 587 ... (4 Replies)
Discussion started by: someone123
4 Replies

2. UNIX for Dummies Questions & Answers

Awk counting lines with field match

Hi, Im trying to create a script that reads throught every line in a file and then counts how many lines there with a certain field that matches a input, and also ausing another awk it has to do the same as the above but to then use sort anduniq to get rid of all the unique lines with another... (8 Replies)
Discussion started by: fredted40x
8 Replies

3. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

4. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

5. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

8. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

9. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

10. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies
All times are GMT -4. The time now is 07:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy