file lookup


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers file lookup
# 1  
Old 12-10-2003
file lookup

I need to do a text lookup for multiple records against a file that contains a key and two results. I found this code:

str=`awk '$1 == search' search=$1 lkup.tbl

It's a little tricky because the first $1 is the key variables location in the lkup.tbl and the second $1 is the parameter passed to the shell.

This works fine if I call it from a shell passing it a key. However I need to do this for thousands of records at a time and can't figure out how to get it to work inside of a awk function.

Any suggestions on how to do this lookup? I searched the site and couldn't find any prior entries on lookups. Thanks, Gill
# 2  
Old 12-10-2003
can you provide us a snipit of data to work with, and an example of how you want it to be displayed on screen?
# 3  
Old 12-10-2003
01077011300 1.2556 0.9865

The 11 byte variable is a location code. They come from the census. I can run my data through an address scrubber and get this information. Then I need to perform the lookup on the data above to get the two percents and add them to my record. I don't need to display anything on my screen. Gill
# 4  
Old 12-10-2003
Things i am assumeing:

1) you have a text file in the format:
KEY Var1 Var2
01077011300 1.2556 0.9865

2) after you get this information (for a specific client you request) you want to update a DB of somesort?


The portion of your question i am not understanding is:
"Then I need to perform the lookup on the data above to get the two percents and add them to my record. "

What kind of record are you updateing? Where do you want this data to go? what format is the "record" in? is it also in the same format as the data file?

with the code below it just prints it to STDOUT. you can easily redirect it to a file or insert it into a db.

Code:
#!/usr/bin/perl -w
#KEY Var1 Var2
#01077011300 1.2556 0.9865
#
# to use the program ./test.plx <filename> <client code you want to search for in file>

use strict;

my ($filename, $cli_code)=@ARGV;
open (FILE,"$filename") or die "Cant open file ($!)\n";

foreach (<FILE>) {
        if (/^$cli_code/) {
                chomp;
                my ($key, $var1, $var2) = split();
                print "Key: $key Var1: $var1 Var2: $var2\n";
        }
}

[qgatu003]$./test.plx datafile 01077011300
Key: 01077011300 Var1: 1.2556 Var2: 0.9865
Key: 01077011300 Var1: .2556 Var2: 10.9865
[qgatu003]$cat datafile
01077011300 1.2556 0.9865
01077011301 1.234 23.3432
01077011300 .2556 10.9865


Last edited by Optimus_P; 12-10-2003 at 06:15 PM..
# 5  
Old 12-10-2003
Thanks for your reply, I should have written that better. What's happening is I'm running a bourne shell script that sends data and a trigger file to another system that runs my data through an outside vendors system. That system use to return both the key and the two percentages. In the future it will only return the key but they supplied the text file above so I would be able to generate those two percentages by performing a lookup on the key or first field.

I'm manipulating that data in an awk script when I get it back and I was hoping to be able to do the lookup inside of the awk script although I can do it separately if needed.

My problem is the awk command above does exactly what I want it too but I can't figure out how to make it work inside an awk script with a variable. I sort my returned file into a pipe into the awk script.

So to summarize I need to take a file that has many records and add the two percentages to each record based on the location code. Once the data is in the correct format I just move the file to a particular location and a java app takes over and sticks it in sybase.

Thanks, Greg
# 6  
Old 12-10-2003
ok i think i see what your trying to do now.

sorry but i only use the most basic forms of awk. hopefully someone else can give you a hand.

if you want to investigate a completely perl route we can prolly pound something out.

i suppose we would need:
1) test data (you supplied that above)
2) list the ways your minipulateing the data
3) show the format you want the data in the file you pass to the java app.

var1=1
var2=2
just a question. are you adding var1+var2?

so it looks like:
location_code1 3
location_code2 5
....
# 7  
Old 12-12-2003
I ran out of time and the process goes through a sas step right after this so I just fixed the variables there. It's real easy to do lookups in sas.

To answer you question no I don't add the variables together I just need to pass them on and financial people make decisions based on them.

If anyone out there knows how I could have implemented this in awk I'd still like to know.

Thanks, Gill
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Lookup value from another file

Hi Experts, I need you help in coding the below scenario in unix. Please help Lets say i have below 2 records in my file1 a;b;c a;d and in file 2 , i have the below information a:alpha b:beta c:code d:delta then i want to lookup on file2 and replace my content in file... (2 Replies)
Discussion started by: ashishagg2005
2 Replies

2. Shell Programming and Scripting

Lookup Within a file

A text file has two logs of same event & both logs have to be correlated into a single line before same can be processed further RAW OPERATING LOG Date Month Year Time Event 10 JAN 2014 1000 4 11 APR 2013 1230 2 12 FEB 2014 ... (7 Replies)
Discussion started by: newageBATMAN
7 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

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

5. Shell Programming and Scripting

Lookup file

Hi, need your help to lookup these 2 files main.txt RNPMS01,PMS717W_Marasi,CXP9016141/1_R7G04,EXECUTING RNPMS01,RAP765W_BakaranBatu,CXP9014346/1_R6AG03,EXECUTING RNPMS01,RNPMS01,CXP9014711/2_R5Z,EXECUTING RNPMS01,TBT510W_Bandar_Utama,CXP9014346/1_R6AG03,EXECUTING... (8 Replies)
Discussion started by: singgih
8 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. Shell Programming and Scripting

Lookup file

I have two files ,File1 lookup to file2 based on 1st&3rd against 1st and 2nd column and produce the following o/p File1 450000|USD|USD 450006|GKSGD|SGD 450002|XSGD|SGD File2 ----- 450000|USD|2000.00|10000.000 450006|SGD|1000.200|3000.000 450002|SGD|3000.000|20000.00 O/p ... (4 Replies)
Discussion started by: mohan705
4 Replies

8. Shell Programming and Scripting

Lookup on a file

Hi, I have the following requirement. I have one lookup file which contains 15 columns and 7000 records. Ex: 123,MEDICA,134,145,1178,123,678,345,2345,HP,COL,K12,SR,OX,78919 I have input file which contains 14 columns and 20 million records.Some times the record count is more... (4 Replies)
Discussion started by: ukatru
4 Replies

9. UNIX for Advanced & Expert Users

Lookup on a file

Hi, I have the following requirement. I have one lookup file which contains 15 columns and 7000 records. Ex: 123,MEDICA,134,145,1178,123,678,345,2345,HP,COL,K12,SR,OX,78919 I have input file which contains 14 columns and 20 million records.Some times the record count is more than 20... (1 Reply)
Discussion started by: ukatru
1 Replies

10. 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
Login or Register to Ask a Question