Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Help with AWK - Compare a field in a file to lookup file and substitute if only a match Post 302712119 by migurus on Monday 8th of October 2012 03:46:18 PM
Old 10-08-2012
you would need to add else clause to situation when $3 in a is not found, such as below

Let me reformat the one-liner into a structured form:
Code:
gawk 'BEGIN {FS=OFS="|"}
{
        if(NR==FNR){
                a[$1]=$2;
                next
        }
        if($3 in a){
                $3=a[$3];
                print $0
        }
         else {
                print $0
        }
}'


Last edited by migurus; 10-08-2012 at 04:47 PM.. Reason: typo
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

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

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

4. Shell Programming and Scripting

File field to replace lookup from another file

Hi All, I don't know how to fast do this field replace that need lookup from another file to form the update result:confused: I want to do it by general shell script Can anyone help to solve it ? Thanks for your kindly reply in advance. CK (0 Replies)
Discussion started by: ckwong99
0 Replies

5. UNIX for Dummies Questions & Answers

Lookup field in map file

Hi, I have two questions which I would massively appreciate help with. 1. I am trying to insert a field into a file similar to the vlookup function in excel. In column 2 is a gene id for which i would like to insert the full name in the adjacent column. I have a map file (map.file) which... (1 Reply)
Discussion started by: genehersh
1 Replies

6. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

7. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

8. Shell Programming and Scripting

awk to print field from lookup file in output

The below awk uses $3 and $4 in search as the min and max, then takes each $2 value in lookup and compares it. If the value in lookupfalls within the range in searchthen it prints the entire line in lookup/ICODE]. What I can't seem to figure out is how to print the matching $5 from search on that... (4 Replies)
Discussion started by: cmccabe
4 Replies

9. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

10. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies
SC_TRACEDIFF(1) 					    BSD General Commands Manual 					   SC_TRACEDIFF(1)

NAME
sc_tracediff -- display traceroute paths where the path has changed. SYNOPSIS
sc_tracediff [-a] [-m method] [-n] file1.warts file2.warts DESCRIPTION
The sc_tracediff utility displays pairs of traceroutes to a destination where the path has changed. It takes two warts files as input and displays paths where a hop differs by its address. The options are as follows: -a dump all traceroute pairs regardless of whether they have changed. -m method specifies the method used to match pairs of traceroutes together. If dst is specified, traceroutes are matched if the destination IP address of both traces are the same. If userid is specified, traceroutes are matched if the userid field of both traces are the same. If dstuserid is specified, traceroutes are matched if the destination IP address and userid fields are the same. By default, the destination IP address is used. -n names should be reported instead of IP addresses, where possible. sc_tracediff can be useful in network monitoring to identify when a forward IP path has changed. In this scenario, it is recommended that Paris traceroute is used with the same UDP source and destination ports for each execution of scamper so that only paths that have changed are identified, not merely alternate paths visible due to per-flow load-balancing. By default scamper uses a source port based on the process ID, which will change with each execution of scamper. EXAMPLES
The command: scamper -O warts -o file1.warts -c 'trace -P udp-paris -s 31337' -f list.txt collects the forward IP paths towards a set of IP addresses found in list.txt using 31337 as the UDP source port value. If the above command is adjusted to subsequently collect file2.warts, then we can identify paths that have subsequently changed with the command: sc_tracediff file1.warts file2.warts If Paris traceroute with ICMP probes is preferred, then the following invocation of scamper is appropriate: scamper -O warts -o file1.warts -c 'trace -P icmp-paris -d 31337' -f list.txt In this case, scamper uses 31337 as the ICMP checksum value in each probe. SEE ALSO
scamper(1), B. Augustin, X. Cuvellier, B. Orgogozo, F. Viger, T. Friedman, M. Latapy, C. Magnien, and R. Teixeira, Avoiding traceroute anomalies with Paris traceroute, Proc. ACM/SIGCOMM Internet Measurement Conference 2006. AUTHOR
sc_tracediff is written by Matthew Luckie <mjl@luckie.org.nz>. BSD
April 21, 2011 BSD
All times are GMT -4. The time now is 07:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy