AWK lookup not finding match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK lookup not finding match
# 1  
Old 10-27-2010
AWK lookup not finding match

Hello everyone,
I have been struggling with the following situation, I think I am doing something wrong, can anyone help?
I have 2 comma separated files, the first is a look-up table that will supply the phone number based on the customer id, the second is a file containing customers and their id.
I would like to generate a file with customer name an phone number for each customer.
No matter what I try I do not seem to find proper matches. The code I have always runs into the 'not matched' section and generated the 'Error' string as printout for all of my incoming records.
When I will manage to see the matching occur then I will introduce the proper PRINT to gather all the data andl place that onto the output, for now I am just trying to see if I am actually able to match.
I also included the for loop at the end just to see I had populated the array properly .... everything to my eyes seems ok, yet I cannot get the matches to occur. Additionally can I also avoid the displaying of all the lines getting processed to go to the screen, I do not see which section of the code is doing that?

Hope someone can help.
Thanks in advance!

File uno contains:
Code:
000000030292,4121234567
000000031064,4121234444
000000031125,4122223474

File due contains:
Code:
000000030292,NAME1, LASTNAME1          ,OP      ,TESTING1     ,OP           ,FRI,10/29/2010,1100 ,FT,ENG
000000031064,NAME2, LASTNAME2          ,OP      ,TESTING2     ,OP           ,FRI,10/29/2010,1600 ,FT,ENG
000000031125,NAME3, LASTNAME3          ,OP      ,TESTING3     ,OP           ,FRI,10/29/2010,1500 ,FT,ENG

Code:
awk 'FS=","
     FILENAME=="uno" {cd1[$1]=$2}
     FILENAME=="due" {
     if ( $1 in cd1)
      	{
      	    print $0" Found" > "outtel"
  
       	}
       	else
       	{
     	     print $1"Error" > "outtel"
  
       	}
} END {for (var in cd1){print var"---"cd1[var]"-" >"outtel"}}' due uno

This is the output I get in outtel:
Code:
000000030292Error
000000031064Error
000000031125Error
000000031125---4122223474-
000000031064---4121234444-
000000030292---4121234567-

# 2  
Old 10-27-2010
Code:
nawk -F, 'FNR==NR{f1[$1]=$2;next} $1 in f1 {f2[$1]=$2} END {for (i in f2) print f2[i], f1[i]}' OFS=, uno due

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-27-2010
I do not have access to nawk, would this code be portable to regular AIX awk?
Thanks!
# 4  
Old 10-27-2010
Quote:
Originally Posted by gio001
I do not have access to nawk, would this code be portable to regular AIX awk?
Thanks!
try it - just use plain awk
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 10-27-2010
@OP I think you have the input files reversed..
Alternative awk:
Code:
awk -F, 'NR==FNR{T[$1]=$2; next}$0=$2 FS $3 FS T[$1]' uno due > outtel

Code:
NAME1, LASTNAME1          ,4121234567
NAME2, LASTNAME2          ,4121234444
NAME3, LASTNAME3          ,4122223474

This User Gave Thanks to Scrutinizer 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

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk base lookup of best match strings

Hi, I'm new to scripting and unable to find out a way to perform the below task. Request help in finding out a way to accomplish this. File one consists of some numbers/string which i need to lookup against file 2 and fetch the best match results in output. If best match is not present in... (3 Replies)
Discussion started by: suraj016
3 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

[Solved] Lookup a file and match the contents

Hi, I appreciate all who have been very helpful to me in providing valuable suggestions and replies. I want to write a script to look up a file and match the contents. Let me go through the scenario. Lets say i have two files Content file: abc, bcd, adh|bcdf|adh|wed bcf, cdf,... (2 Replies)
Discussion started by: forums123456
2 Replies

6. UNIX for Dummies Questions & Answers

finding the nth match

I have a file that has information for a person....each person gets 3 or more lines to describe them. I was hoping to match person 1, then person 2.....BUT I do not know how to tell grep I only want the first (2nd, 3rd or nth) match. The alternative is doing line by line logic, which is fine... (8 Replies)
Discussion started by: countryStyle
8 Replies

7. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

8. UNIX for Dummies Questions & Answers

finding all files that do not match a certain pattern

I hope I'm asking this the right way -- I've been sending out a lot of resumes and some of them I saw on Craigslist -- so I named the file as 'Craigslist -- (filename)'. Well I noticed that at least one of the files was misspelled as 'Craigslit.' I want to eventually try to write a shell... (5 Replies)
Discussion started by: Straitsfan
5 Replies

9. Shell Programming and Scripting

Finding exact match string

Hi All, I'm writing unix script, it should find exact matching in search string. Looks simple but when i started i'm stuck to find the exact match character string. The unix script reads the records from DB Table. The table will have values something likes these Feed : A Feed File name :... (3 Replies)
Discussion started by: luckybalaji
3 Replies

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