Conditional Vlookup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional Vlookup
# 1  
Old 05-11-2018
Question Conditional Vlookup

Hi everyone,

I need to replace values of column 2 array1 with values of column 2 array2 based on a lookup of column 4 value, but only return a value IF the values in column 1 of BOTH array1 and array2 match, otherwise keep original value in column 2 of array1. Both files are tab delimited text files and I'm using Ubuntu 16.04.

For example, here is array1:

Code:
1    ab123    0.02013    1111    T    C
1    ab124    0.026621    1112    T    C
1    ab125    0.027256    5423    A    G
1    ab126    0.028999    1245    G    T
1    ab127    0.029335    1246    C    A
1    ab128    0.029367    1247    A    G
1    ab129    0.029785    1248    G    T
2    ab130    0.044493    1111    C    A
2    ab131    0.044524    1247    C    T
2    ab132    0.044571    7542    C    T
2    ab133    0.044629    1451    C    A
2    ab134    0.044636    1452    C    T
2    ab135    0.044993    1453    C    T


Here is array2:

Code:
1    rs3094315    0.02013    1111    G    A
1    rs6687776    0.026621    1112    T    C
1    rs9442380    0.027256    1113    T    C
1    rs11260549    0.028999    1245    A    G
1    rs2887286    0.029335    1246    C    T
1    rs3813199    0.029367    1247    A    G
1    rs6685064    0.029785    1248    T    C
2    rs4648808    0.044493    1111    T    C
2    rs3128291    0.044524    1247    A    G
2    rs3128296    0.044571    1450    G    T
2    rs424079    0.044629    1451    C    A
2    rs2257182    0.044636    1452    C    T
2    rs263526    0.044993    1453    C    T


Here is the desired output:
Code:
1    rs3094315    0.02013    1111    T    C
1    rs6687776    0.026621    1112    T    C
1    ab125    0.027256    5423    A    G
1    rs11260549    0.028999    1245    G    T
1    rs2887286    0.029335    1246    C    A
1    rs3813199    0.029367    1247    A    G
1    rs6685064    0.029785    1248    G    T
2    rs4648808    0.044493    1111    C    A
2    rs3128291    0.044524    1247    C    T
2    ab132    0.044571    7542    C    T
2    rs424079    0.044629    1451    C    A
2    rs2257182    0.044636    1452    C    T
2    rs263526    0.044993    1453    C    T



Notice that since there is no equivalent column 4 values for rows 3 and 8, I want to keep the original column 2 values in array1


I used the following code and it works fine, except for it does not condition on values of column 1 of both array1 and array2 matching. I need to condition the lookup on values of column 1 of both array1 and array2 matching:
Code:
awk 'NR == FNR {REP[$4] = $2; next} $4 in REP && FNR > 0 {$2 = REP[$4]; CNT++} 1; END {print CNT}' OFS="\t" array2 array1  > output


Thanks in advance
# 2  
Old 05-12-2018
Would
Code:
awk 'NR == FNR {REP[$1,$4] = $2; next} ($1,$4) in REP  {$2 = REP[$1,$4]; CNT++} 1; END {print CNT}' OFS="\t" file2 file1

do what you need?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-12-2018
Quote:
Originally Posted by RudiC
Would
Code:
awk 'NR == FNR {REP[$1,$4] = $2; next} ($1,$4) in REP  {$2 = REP[$1,$4]; CNT++} 1; END {print CNT}' OFS="\t" file2 file1

do what you need?

Awesome Rudy, you're a genius Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Vlookup not using awk

Hi I just want again to ask for help on what command to use to vlookup f1 group name in "/etc/group" matching f3 of it to "/etc/passwd" f4. I do need to display group name in the output of /etc/passwd without using awk or NR==FNR command. thank you while IFS=: read -r f1 f2 f3 f4 f5 f6 f7... (4 Replies)
Discussion started by: joonisio
4 Replies

2. UNIX for Beginners Questions & Answers

Vlookup on 2 files - inserting vlookup command on another command

Hello, i am trying to print group name column(etc/group) on script (etc/passwd) since group name is not listed on etc/passwd columns. Im trying to do a vlookup. but i cant figure out how i can insert the vlookup command FNR==NR inside the print out command or the output. I also tried exporting... (2 Replies)
Discussion started by: joonisio
2 Replies

3. Shell Programming and Scripting

Vlookup and remove line

Ignore list test_1* test_2 test_5 Source file i need to remove test_1_buddy i need to remove test_1_buddy2 i need to remove test_1 i need to keep_test_10 i need to keep_test_11 i need to remove test_5 i need to remove test_2 (3 Replies)
Discussion started by: ranjancom2000
3 Replies

4. Shell Programming and Scripting

Vlookup in Linux

Hello Everybody I am looking for vlookup like functionality in Linux since two files I have are very big(1000MB each) and its not opening completely in excel. Here the requirement file1 11,12,13 16,14,12 28,21,22 22,23,24 file 3 18,16,16 14,12,12 23,22,24 16,11,13 here... (8 Replies)
Discussion started by: shanul karim
8 Replies

5. Shell Programming and Scripting

Vlookup using awk

Hi folks, awk 'NR==FNR {m=$0; next} $1 in m{$0=m} {print}' file2 file1 Works a charm for a vlookup type query, sourced from https://www.unix.com/shell-programming-and-scripting/215998-vlookup-using-awk.html However my column content has white spaces and numbers. Example file1 The Man... (6 Replies)
Discussion started by: pshields1984
6 Replies

6. UNIX for Dummies Questions & Answers

Command for vlookup function

Hello experts, I have large text files that need to be arranged using a function like excel's vlookup. I have been playing with awk command but didn't really come up with a solution. Could anyone please help me out? Below are my datasets and expected output. Any help would be greatly... (8 Replies)
Discussion started by: makelifeeasier
8 Replies

7. Shell Programming and Scripting

Shell Scripting Vlookup

I am developing shell script on Linux OS and I have two files.Data in each file is like : File1 : A B C E F G X Y Z File 2: A C 12 E G 22 X Z 41 I need if first and third column entries ( $1 & $3) of File1 in same row matches with first & second column... (3 Replies)
Discussion started by: suneet17
3 Replies

8. Shell Programming and Scripting

Vlookup using awk

Hello, I am trying to use vlookup (Excel function) using awk but there is some problem :( --file1-- ABC123 101F X1 A $P=Z X2 A $P=X X3 B $P=F X4 C $P=G MNK180 END --file2-- X1 A_t $P=Z X2 A_t $P=X X3 B_u $P=F X4 C_o $P=G (2 Replies)
Discussion started by: young
2 Replies

9. Shell Programming and Scripting

vlookup files

hi frnds i have 2 files. 1st is dddd and 2nd is ssss ==> dddd <==: 1,charit 2,gilhotra ==> ssss <==: 1,sajan 2,doda 3,hello and i want o/p ...mean join and vlookup both files sajan,charit (4 Replies)
Discussion started by: dodasajan
4 Replies

10. Shell Programming and Scripting

VLOOKUP utility in UNIX

Kindly help me to build the script similar to VLOOKUP function of MS Excell. (5 Replies)
Discussion started by: hjoshi
5 Replies
Login or Register to Ask a Question