Vlookup in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Vlookup in Linux
# 1  
Old 08-15-2017
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

Code:
 11,12,13
 16,14,12
 28,21,22
 22,23,24

file 3

Code:
 18,16,16
 14,12,12
 23,22,24
 16,11,13

here I need to do we lookup using 3rd column of file3

output file

Code:
 11,12,13  NA
 16,14,12  14,12,12
 28,21,22  NA
 22,23,24  23,22,24

I have tried below vlookup but its giving wrong output

Code:
$ cat vlookup.awk
 FNR==NR{
 a[$1]=$2
 next
 }
 { if ($1 in a) {print $1, a[$1]} else {print $1, "NA"} }

Code:
$ awk -f vlookup.awk file3 file1 | column -t
 11,12,13 NA
 16,14,12 NA
 28,21,22 NA
 22,23,24 NA



Request for your help to resolve this.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!


---------- Post updated at 01:56 PM ---------- Previous update was at 01:21 PM ----------

thanks as advice code tag added..

Last edited by RudiC; 08-15-2017 at 04:01 PM.. Reason: Added CODE tags.
# 2  
Old 08-15-2017
Try
Code:
awk '
FNR==NR {a[$1] = $0
         next
        }

        {print $0, a[$2]?a[$2]:"NA"
        }
' FS=, file3 file1
11,12,13 NA
16,14,12 14,12,12
28,21,22 NA
22,23,24 23,22,24

# 3  
Old 08-15-2017
Hello RudiC

Thanks for the reply..

Just want to know whether this code work same for below files as well
Mean column 3 from CBUEBU_REF as reference.

I tried and got all NA

Code:
$ head -n 10 CBUEBU_REF
754123450,820017123454441,7512345201
701234512,820017312345440,7012345122
701234544,820017312345439,7012345443
703123450,820123458284423,7012345401
751234503,820017301234529,7512345032
751234562,820017312345928,7561234523
701234581,820017312345133,7012345811
753123458,820017123457041,7531234582
753123455,820017123459194,7531234553
701234522,820017123453601,7012345221

Code:
$ head -n 10 CUDB_REF

751234592,820017123451132,7512345921
701234559,820017312345133,7001123452
701234578,820017301234534,7001234583
700123454,820011234581410,7012345941
701234514,820017312345411,7012345142
701234574,820017123451412,7081234543
751234593,820017301234565,7581234531
755123453,820017312345909,7512345932
701234554,820017301234532,701234543
751234586,820017123456114,7551234561


Last edited by hicksd8; 04-27-2020 at 09:50 AM..
# 4  
Old 08-15-2017
So what would the desired output be? Which field should match which column?
# 5  
Old 08-15-2017
Here are the files and need to compare column 3 with the same logic

Code:
 file3
754123450,820017123454441,7512345201
701234512,820017312345440,7012345122
701234544,820017312345439,7012345443
703123450,820123458284423,7012345401
751234503,820017301234529,7512345032
751234562,820017312345928,7561234523
701234581,820017312345133,7012345811
753123458,820017123457041,7531234582
753123455,820017123459194,7531234553
701234522,820017123453601,7012345221


Code:
 file1
751234512,821234501068703,7512345123
712345279,820017121234519,7123452791
712345222,820017301234501,7051234521
723458177,820017301234531,7521234572
751234520,820017312345441,7512345201
756123452,820017312345928,7512345623
751234545,820017123459194,7512345453
701234581,820017123459133,7012345811
701234539,820017123454493,7123459393
712345229,820017123459186,7123452291

The desired output files are attached

Last edited by hicksd8; 04-27-2020 at 09:52 AM..
# 6  
Old 08-15-2017
Applying the corrected script (replaced all field references with $3) to your most recent input samples yields exactly your desired output files. The former samples did NOT have any entry in field 3 in common, so "NA" would be correct.
# 7  
Old 08-15-2017
ok, What is the corrected code now for these input and outputs..
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

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... (2 Replies)
Discussion started by: Geneanalyst
2 Replies

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

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

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

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

8. Shell Programming and Scripting

Vlookup functionality in Bash

Hi please help in the below request Input File 1: a,1 b,2 c,3 d,4 e,5 f,6 Input File 2: 2 5 3 1 Output Required: b 2 e 5 c 3 (3 Replies)
Discussion started by: ravin
3 Replies

9. Shell Programming and Scripting

merge files like VLOOKUP

I would like to merge data from a reference file and a data file to produce a new output file as shown below. Reference file,data file,output file a , b 2 , a 0 b , d 4 , b 2 c , , c 0 d , , d 4 e, , e 0 (3 Replies)
Discussion started by: godzilla07
3 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