Lookup in Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Lookup in Unix
# 1  
Old 12-12-2011
Lookup in Unix

Hi,

I have an input file which contain below records:
Code:
a,1
b,2
c,3
a,10
b,34

i have a reference file which contains below records:
Code:
a,AA
b,BB
c,CC

My required output is :
Code:
a,1,AA
b,2,BB
c,3,CC
a,10,AA
b,34,BB

How we can achieve this in unix?

Thanks
# 2  
Old 12-12-2011
Hi,

quick & dirty solution:

Code:
for currline in `cat input.txt`
do 
   ref=`echo ${currline} | cut -d, -f1`
   string_from_ref=`grep ${ref} ref.txt | cut -d, -f2`
   echo "${currline},${string_from_ref}" >> output.txt
done

you can further optimize to check for errors (i.e. string_from_ref stores no value, etc...)

see ya
fra

Last edited by frappa; 12-12-2011 at 02:21 PM.. Reason: added some clarification
This User Gave Thanks to frappa For This Post:
# 3  
Old 12-12-2011
With awk:
Code:
zaxxon@saburac ~
# cat f2
a,AA
b,BB
c,CC

zaxxon@saburac ~
# cat f1
a,1
b,2
c,3
a,10
b,34

zaxxon@saburac ~
# awk -F, 'NR==FNR{_[$1]=$2; next} _[$1] {print $1,$2,_[$1]}' OFS=, f2 f1
a,1,AA
b,2,BB
c,3,CC
a,10,AA
b,34,BB

# 4  
Old 12-12-2011
Neither of these ideas are right, but they just illustrate that the "join" command needs sorted input.
Code:
join -j 1 -t ',' file1 file2
a,1,AA
b,2,BB
c,3,CC

sort <file1 >file1.sor
sort <file2 >file2.sor
join -j 1 -t ',' file1.sor file2.sor
a,1,AA
a,10,AA
b,2,BB
b,34,BB
c,3,CC


But if I cheat and sort the output on the second field:
Code:
sort <file1 >file1.sor
sort <file2 >file2.sor
join -j 1 -t ',' file1.sor file2.sor | sort -t',' -k2n
a,1,AA
b,2,BB
c,3,CC
a,10,AA
b,34,BB


Last edited by methyl; 12-12-2011 at 02:28 PM..
This User Gave Thanks to methyl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array V-Lookup using UNIX bash

Hey everyone, I am trying to extract column values from a column in a tab-delimited text file and overlay them in a 2nd tab-delimited text file using a V-lookup type script in Unix bash. These are the 1st few rows of the 1st input file IN1: rsid chromosome position allele1 ... (10 Replies)
Discussion started by: Geneanalyst
10 Replies

2. Shell Programming and Scripting

Lookup field values in two fixed format file in UNIX - not working

I have 2 fixed length files input#1 & input#2. I want to match the rows based on the value in position 37-50 in both files (pos 37-50 will have same value in both files). If any matching record is found then cut the value against company code & Invoice number from input file #1 (position 99 until... (3 Replies)
Discussion started by: Lingaraju
3 Replies

3. Shell Programming and Scripting

Lookup name from another file

Hi All, I want to lookup name for an id in col2 input from another file and add the name to each line. Input 1 comp100001_c0_seq1 At1g31340 30.40 569 384 11 3 1673 313 834 7e-62 237 comp100003_c0_seq1 At1g35370_2 35.00 80 50 ... (7 Replies)
Discussion started by: gina.lizar
7 Replies

4. UNIX for Dummies Questions & Answers

Checking for unix hosts that do not have reverse lookup

Hi there i am not sure how to explain my problem. i need to run a script to give me the results of all my unix hosts that do not have reverse lookup activated (for lack of a better word), i need to give this to out Server guys to add it part of the AD rules. So what i need is a script to... (0 Replies)
Discussion started by: brian112
0 Replies

5. Shell Programming and Scripting

lookup

I have a lookup file in unix say /data/lkp.dat (First line is header and space delimited) and the content is shown below. Another file which contains the job_name and rec_count lets say /data/data_file.dat(no header pipe delimited file). Now i want to do a lookup on job_name and my output should... (3 Replies)
Discussion started by: dr46014
3 Replies

6. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

7. UNIX for Dummies Questions & Answers

lookup in unix

i am having one file which is of CSV format with two fields client_id and client_nbr.The sample data is lke below ABC,1250 CDE,1520 EFG,1000 PQR,1800 The client nbr for these clients change frequently.So i want to create one lookup file every week for the changed client and run a script... (3 Replies)
Discussion started by: dr46014
3 Replies

8. Shell Programming and Scripting

lookup in unix

Hi All I have got a fixed length file of 80bytes long.The first 4bytes of each record represents a client_number.I need to modify the client number based on another lookup file. The lookup file contains 2 fields and a comma delimited file.The first line of the lookup file contains the header... (5 Replies)
Discussion started by: dr46014
5 Replies

9. UNIX for Dummies Questions & Answers

Lookup with a file

Hi All, i have a variable which has a value in it. RETAILER='JEWL' i have a text file. Name: file.txt file.txt ________ WLG 150 JEWL 60 CVS 240 FLN 120 WND 120 I am trying to write a korn script.the script, based on the value in the RETAILER will do a look up against the... (5 Replies)
Discussion started by: pavan_test
5 Replies

10. UNIX for Dummies Questions & Answers

Unix 8.2 and reverse Lookup

We have Unix configured as our external DNS, forward DNS is working properly, however Reverse lookup is not working. Any idea what the problem is? I have checked the named.boot and .rev file and everything seems to be correctly. However it appears that the reverse zone file in the named.boot... (2 Replies)
Discussion started by: cassy
2 Replies
Login or Register to Ask a Question