Sponsored Content
Full Discussion: Problem with lookup in awk
Top Forums Shell Programming and Scripting Problem with lookup in awk Post 302955863 by migurus on Tuesday 22nd of September 2015 03:47:02 PM
Old 09-22-2015
Problem with lookup in awk

I need to add new ID to the file with old ID (column 7), I collected old ID / new ID pairs in a lookup file and I am trying to use awk to do the job, but something is not clicking.

My input file
Code:
ABC| 107|1440589221| -118.117167|   33.986333|10|  497476|1
ABC| 125|1440591215| -118.181000|   34.046833|10|  495713|1

I need to get this
Code:
ABC| 107|1440589221| -118.117167|   33.986333|10|  497476|1|10636872
ABC| 125|1440591215| -118.181000|   34.046833|10|  495713|1|10640836

My lookup file
Code:
497476|10636872
495713|10640836

My awk code (with all the debug I put in it)
Code:
awk 'BEGIN{old_key=0; FS=OFS="|"}
{
        if (NR == FNR) {
                old_key = $1;
                printf "adding to array: arr[%i] = %s\n", old_key, $2;
                arr[old_key] = $2;
        }
        else {
                old_key = $7;
                printf "\t\tlooking for:%d\n", old_key
                if(old_key in arr) {
                        new_key = arr[old_key];
                        printf "\t\tfound %d\n", arr[old_key];
                }
                else {
                        printf "\t\tNOT found %d\n", old_key;
                        new_key = -1;
                }
                 print $0, new_key;
        }
}
END{
 print "END";
 for(x in arr) print x, arr[x]
}'

Run results:
Code:
adding to array: arr[497476] = 10636872
adding to array: arr[495713] = 10640836
                looking for:497476
                NOT found 497476
ABC| 107|1440589221| -118.117167|   33.986333|10|  497476|1|-1
                looking for:495713
                NOT found 495713
ABC| 125|1440591215| -118.181000|   34.046833|10|  495713|1|-1
END
497476|10636872
495713|10640836

I'd appreciate it very much if someone can point me in the right direction, thanks in advance.

Last edited by migurus; 09-22-2015 at 04:48 PM.. Reason: typo
 

10 More Discussions You Might Find Interesting

1. IP Networking

reverse lookup file problem

I'm trying to create a reverse lookup file. Below are the error messages I get in the messages file, when I start named. Below the error messages is a copy of the reverse lookup file I'm trying to use. I'm using Bind version 8.1.2. Would someone recommend the correct values and if you see any... (2 Replies)
Discussion started by: Westy564
2 Replies

2. Shell Programming and Scripting

file Lookup using awk

Hi All, I have two files file1 and file2(lookup file).I need to map more than one keyfields of file1 with file2.how can we achieve it using awk. file1(max 2.2 million records) -------------------------- 680720|680721|077 680720|680721|978 680721|680722|090 file2(no idea about the... (1 Reply)
Discussion started by: jerome Sukumar
1 Replies

3. 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

4. Shell Programming and Scripting

Multiple file lookup using awk

I want to lookup filea with fileb,filec and filed. If entry in filea exist in fileb and filec mark Y and then if entry in filea exist in filed mark as Y. Final output should have all the entries from filea. This prints only matching entries from file a in fileb i want all entries from... (9 Replies)
Discussion started by: pinnacle
9 Replies

5. Shell Programming and Scripting

variable lookup problem in shell script

Hi I have one properties file containing as $INSTALL_BASEPATH/mssages/commonmessages_default.properties $INSTALL_BASEPATH/resource/configurationBundle.properties and $INSTALL_BASEPATH is set in .bash_profile but from shell script when I read this file and use in copy statement then it... (7 Replies)
Discussion started by: mnmonu
7 Replies

6. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. UNIX for Advanced & Expert Users

NIS Group Lookup Problem

I'm running a NIS on an network of Ubuntu 8.04 linux systems. I'm seeing a weird problem where the 'id' command is not returning all the groups I am a member of. For example: alex@client $ id -Gn localgroupA localgroupB nisgroup1 nisgroup2 nisgroup4 alex@client $ id -Gn alex nisgroup1... (0 Replies)
Discussion started by: vertigo23
0 Replies

8. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: gio001
4 Replies

9. Shell Programming and Scripting

awk to lookup value in one file in another range

I am trying to update the below awk, kindly provided by @RavinderSingh13, to update each line of file1 with either Low or No Low based on matching $2 of file1 to a range in $2 and $3 of file2. If the $2 value in file1 matches the range in file2 then that line is Low, otherwise it is No Low in the... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 04:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy