Vlookup in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Vlookup in Linux
# 8  
Old 08-15-2017
Isn't that clear?Try
Code:
awk '
FNR==NR {a[$3] = $0
         next
        }

        {print $0, a[$3]?a[$3]:"NA"
        }
' FS=, file3 file1

# 9  
Old 08-16-2017
its very much clear and I have tested and its working fine for my small input files.

Really thankful to you for all your support.

But while testing same logic for bigger input files. I saw some strange behaviour.

like

Code:
$ awk '
> FNR==NR {a[$3] = $0
>          next
>         }
>
>         {print $0, a[$3]?a[$3]:"NA"
>         }
> ' FS=, file8 file9 > FINAL1

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ head -n 10 FINAL1
751234592,820011234501132,7512345921 NA
701234559,820011234501133,7012345592 NA
701234578,820011234501134,7012345783 NA
701234594,820012345481410,7123451941 NA
701234514,820012345481411,7071234542 NA
701234574,820012345481412,7012345743 NA
751234593,820012345771965,7512345931 758123453,820017123451965,7123459931
712345693,820012341234509,7123456932 755123453,821234501234509,7512345932
703123454,820012345802232,7035123453 703123454,820017312345232,7012345543
755123456,8200123454461146,71234520861 NA

whereas "712345692,820017123456132,7123456921" exists in both file8 and file9. So result should not be NA.

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ grep "712345592,820017123451132,7123455921" file8
751234592,820017312345632,7512345921

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ grep "751234592,821234501501132,7123455921" file9
751234592,821234501501132,7123455921

in file8 its in line 1792131 and in file9 its at 1st line.
what could be the reason of this NA now since data is at coloumn 3 but at row 1792131.

anything can improve in code that it include lines upto 3000000 search for vlookup of column 3.

---------- Post updated at 10:33 PM ---------- Previous update was at 04:28 PM ----------

Quote:
Originally Posted by shanul karim
its very much clear and I have tested and its working fine for my small input files.

Really thankful to you for all your support.

But while testing same logic for bigger input files. I saw some strange behaviour.

like

Code:
$ awk '
> FNR==NR {a[$3] = $0
>          next
>         }
>
>         {print $0, a[$3]?a[$3]:"NA"
>         }
> ' FS=, file8 file9 > FINAL1

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ head -n 10 FINAL1
751234592,820011234501132,7512345921 NA
701234559,820011234501133,7012345592 NA
701234578,820011234501134,7012345783 NA
701234594,820012345481410,7123451941 NA
701234514,820012345481411,7071234542 NA
701234574,820012345481412,7012345743 NA
751234593,820012345771965,7512345931 758123453,820017123451965,7123459931
712345693,820012341234509,7123456932 755123453,821234501234509,7512345932
703123454,820012345802232,7035123453 703123454,820017312345232,7012345543
755123456,8200123454461146,71234520861 NA

whereas "75123456,820012345601132,712345921" exists in both file8 and file9. So result should not be NA.

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ grep "723450592,820123451501132,7123455921" file8
723450592,820123451501132,7123455921

Code:
eshaqur@IN00106004 ~/CNG_MSISDN_AUDIT
$ grep "723450592,820123451501132,7123455921" file9
723450592,820123451501132,7123455921

in file8 its in line 1792131 and in file9 its at 1st line.
what could be the reason of this NA now since data is at coloumn 3 but at row 1792131.

anything can improve in code that it include lines upto 3000000 search for vlookup of column 3.

Finally it's solved..Thanks again for support. Actually it's resolved by copy content of input files in text format manually using ultra edit. And then apply your awk command.

Last edited by hicksd8; 04-27-2020 at 10:01 AM..
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