awk - Vlook up functionality


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Vlook up functionality
# 1  
Old 06-01-2013
awk - Vlook up functionality

i am searching how to implement vlookup functionality in AWK

i have file1 as
Code:
100,A
200,B
300,C
600,B
400,C

and file2 as
Code:
A,1
B,2
C,3

and the output should be
Code:
100,A,1
200,B,2
300,C,3
600,B,2
400,C,3

can any help me in this Smilie

Last edited by Scott; 06-01-2013 at 03:53 AM.. Reason: Please use code tags
# 2  
Old 06-01-2013
Is this a homework assignment?
# 3  
Old 06-01-2013
no this is not an assignment
i was trying to implement vlookup functionality and i was not getting

Last edited by janu_11; 06-01-2013 at 02:22 AM..
# 4  
Old 06-01-2013
Try something like:
Code:
awk '
BEGIN { FS = OFS = "," }
FNR == NR { x[$1] = $2; next }
        { print $0, x[$2] }' file2 file1

If you are using a Solaris/SunOS system, change awk in the script above to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 5  
Old 06-19-2013
Thank you very much Don this has worked out
# 6  
Old 06-19-2013
Code:
gawk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$2]}' file2 file1
100,A,1
200,B,2
300,C,3
600,B,2
400,C,3

---------- Post updated at 11:45 AM ---------- Previous update was at 11:42 AM ----------

Quote:
Originally Posted by Don Cragun
Try something like:
Code:
awk '
BEGIN { FS = OFS = "," }
FNR == NR { x[$1] = $2; next }
        { print $0, x[$2] }' file2 file1

If you are using a Solaris/SunOS system, change awk in the script above to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
My code does work too:
Code:
gawk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$2]}' file2 file1

And what does "next" mean in your code? I'm a little confused.
# 7  
Old 06-19-2013
Quote:
Originally Posted by franksunnn
---------- Post updated at 11:45 AM ---------- Previous update was at 11:42 AM ----------
My code does work too:
Code:
gawk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$2]}' file2 file1

And what does "next" mean in your code? I'm a little confused.
The standards describe the meaning of the "next" statement as:
Quote:
The next statement shall cause all further processing of the current input record to be abandoned. The behavior is undefined if a next statement appears or is invoked in a BEGIN or END action.
So, the "next" in my suggested code:
Code:
awk '
BEGIN { FS = OFS = "," }
FNR == NR { x[$1] = $2; next }
        { print $0, x[$2] }' file2 file1

skips to the next input record without evaluating the the last statement in my script. The condition NR>FNR before the last action in your suggested code makes your script produce the same output as my suggested code. My script doesn't look at the last statement when the condition FNR==NR is true; your code always evaluates NR>FNR even when it has already determined that FNR==NR.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Zip -r Functionality

Hi , I've written the following code to zip the big file $dir_temp ="/home/etc/hst zip -r $dir_temp/file_nm.zip $dir_temp/file_nm The zip file has been created . When I try to UNZIP the file with the following command unzip file_nm.zip The file got unzipped but created in the... (3 Replies)
Discussion started by: smile689
3 Replies

2. Shell Programming and Scripting

Adding the email functionality

I have make an menu in which first option is to start and second is to stop the services echo "Please enter the appropriate choice for doing the operations" echo " 1) STOP Services 2) START Services case $choice in 1) ... (4 Replies)
Discussion started by: punpun66
4 Replies

3. UNIX for Dummies Questions & Answers

Command Functionality

Hi everyone, today i need that someone help to understand this particular line of command. So you can explain to me step by step, it will be great. ---------- Post updated at 11:53 AM ---------- Previous update was at 11:51 AM ---------- (9 Replies)
Discussion started by: Newer
9 Replies

4. Shell Programming and Scripting

Trigger functionality in Unix

Hi, I want a script , which searches the log for the term/phrase "JFSnapshotService::systemSnapshot: Starting data capture. This may take awhile depending upon system workload." and if there is some logging like this, it has to mail me that this data capture process is happening., Below is... (2 Replies)
Discussion started by: cratercrabs
2 Replies

5. Shell Programming and Scripting

Pipe Functionality

Hi, I am trying to emulate the '|' functionality through pipe function call. I am passing the o/p of exec in parent as input to the exec in child. The buf is readin the o/p but the exec in child is not working. Can somebody checkout the code and point where am i going wrong or missing something.... (3 Replies)
Discussion started by: amejoish
3 Replies

6. UNIX for Dummies Questions & Answers

using functionality in another ksh

i have a function defined in one ksh i want to use the same functionality in another ksh i am using . ../<ksh name> but it is not picking that functionality what i have to do for the same (2 Replies)
Discussion started by: trichyselva
2 Replies

7. UNIX for Advanced & Expert Users

Need Help with Unix/AIX functionality

I'll start this out by saying that I'm super-new to UNIX and need some help. My setup is as follows: I am currently running 3 UNIX boxes, 2 application servers and 1 db server. The first application server is my production box, the second application server is my test box, and the db server... (1 Reply)
Discussion started by: dkeaton
1 Replies

8. Shell Programming and Scripting

Restartibility Functionality....

Hello, I am trying to write a script that has a option of restarting the script from where it failed. I have to write a script called Batch.sh. This script has to run quite a few sql files as shown below: logcmd.sh -f test1.sql logcmd.sh -f test2.sql logcmd.sh -f test3.sql logcmd.sh -f... (4 Replies)
Discussion started by: rkumar28
4 Replies

9. Shell Programming and Scripting

Sed functionality

I have a few xml files and I want to input say 5 parameters within each file. is it possible to do so with sed? <parameter>A</parameter> <parameter>B</parameter> .... .... And so on. These parameters are meant to go in just inside: <?xml... (2 Replies)
Discussion started by: collern2
2 Replies

10. UNIX for Dummies Questions & Answers

Date functionality

Hi, Could someone help me to get yesterday's date in MMDDYY format. echo `date '+%m%d%y'` is giving me today's date in the above format. Thanks in advance for your help.. Suresh. (1 Reply)
Discussion started by: svannala1
1 Replies
Login or Register to Ask a Question