Shell Scripting Vlookup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripting Vlookup
# 1  
Old 03-21-2013
Shell Scripting Vlookup

I am developing shell script on Linux OS and I have two files.Data in each file is like :

File1 :
Code:
A    B   C 
E     F    G 
X   Y    Z


File 2:
Code:
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 entries ($1 & $2) of File 2 in same row , Then whatsoever is in next column of File 2 (For eg. 12 , 22 , 41 as above) should join as 4th column of File 1.

In the above problem the desired output needed is :

A B C 12
E F G 22
X Y Z 41

Last edited by Franklin52; 03-23-2013 at 10:59 AM.. Reason: Please use code tags
# 2  
Old 03-21-2013
Code:
awk 'NR==FNR{a[$1$3]=$0;next}{print a[$1$2],$3;}' file1 file2

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 03-21-2013
with join
Code:
join -1 3 -2 2 -o "1.1 1.2 2.2 2.3" file1 file2

Sorry, this only looks at the 3rd field in file1 and the 2nd field in file2. Not what the OP wanted.

Last edited by ni2; 03-21-2013 at 10:24 AM.. Reason: Noticed that my post was not what the OP had asked for.
This User Gave Thanks to ni2 For This Post:
# 4  
Old 03-22-2013
Quote:
Originally Posted by guruprasadpr
Code:
awk 'NR==FNR{a[$1$3]=$0;next}{print a[$1$2],$3;}' file1 file2

Guru.
Thanks Guru , It worked.
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 using Ask from specific column shell script

Input file1 frame1,dummy,server1, 00C1C N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1D N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1E N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1F N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C20 N/A RDF1+TDEV RW 51789 frame1,dummy,server1,... (10 Replies)
Discussion started by: ranjancom2000
10 Replies

4. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

8. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

9. Shell Programming and Scripting

Creating Report file with 'vlookup' kind of structure in shell

Hi, I have some files in the following structure. File_a.txt Field_1 Pass Field_2 Pass Field_3 Pass File_b.txt Field_1 Pass Field_2 Fail Field_3 Pass File_c.txt Field_1 Fail Field_2 Pass Field_3 Pass (2 Replies)
Discussion started by: vikaskm
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question