Sponsored Content
Top Forums Shell Programming and Scripting select a record from one file matching from second file using awk Post 302204323 by synmag on Wednesday 11th of June 2008 09:49:44 AM
Old 06-11-2008
Quote:
Originally Posted by era
Oh, I misunderstood your problem description. Here's something which hopefully is closer to what you wanted.

Code:
awk -F , -v acct=root 'NR==FNR { h[$1]++; next; }
{ if($1 == "") $1=host; host=$1; if (! h[$1]) next;
if ($2 == acct) print }' fileb filea

The NR==FNR condition is true while fileb is being read. This is a common awk idiom for reading in a file of auxiliary data before the main processing. The host names in fileb will be used to populate the array h. Then in the main body of the script, if the first field is empty, the value of the host variable will be used for $1. Then the current value of $1 will be remembered in host in case the next line(s) have empty first fields. Then, if the array h does not contain the current host name (meaning it was not present in fileb), the current line is skipped. Finally, if the second field is identical to the variable acct, the line is printed.

(Really old variants of awk do not support passing in variables with the -v option -- if you have this problem, see if you can find nawk or mawk or gawk on your system, or an XPG4 awk. Or you can interpolate the variable directly into the script.)
That's exactly what I wanted. My version of awk didn't support the passing of the variable but I do have nawk and it worked perfectly.

Based on awk documentation I was trying to use the begin block to read the file but couldn't figure out how to read from two files. Would you mind explaining how awk determines which file to read from? Is it the code in the first set of braces {} reads the first file supplied and the second set the second file?

Thank you. I learned a lot from this!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

2. Shell Programming and Scripting

awk-filter record by another file

I have file1 3049 3138 4672 22631 45324 112382 121240 125470 130289 186128 193996 194002 202776 228002 253221 273523 284601 284605 641858 (8 Replies)
Discussion started by: biomed
8 Replies

3. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

4. Shell Programming and Scripting

awk - mixed for and if to select particular lines in a data file

Hi all, I am new to AWK and I am trying to solve a problem that is probably easy for an expert. Suppose I have the following data file input.txt: 20 35 43 20 23 54 20 62 21 20.5 43 12 20.5 33 11 20.5 89 87 21 33 20 21 22 21 21 56 87 I want to select from all lines having the... (4 Replies)
Discussion started by: naska
4 Replies

5. Shell Programming and Scripting

awk print matching records and occurences of each record

Hi all , I have two files : dblp.xml with dblp records and itu1.txt with faculty members records. I need to find out how many dblp records are related to the faculty members. More specific: I need to find out which names from itu1.txt are a match in dblp. xml file , print them and show how many... (4 Replies)
Discussion started by: iori
4 Replies

6. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

7. Shell Programming and Scripting

awk to select lines with maximum value of each record based on column value

Hello, I want to get the maximum value of each record separated by empty line based on the 3rd column of each row within each record? Input: A1 chr5D 634 7 82 707 A2 chr5D 637 6 82 713 A3 chr5D 637 5 82 713 A4 chr5D 626 1 82 704... (4 Replies)
Discussion started by: yifangt
4 Replies

8. Shell Programming and Scripting

awk record matching

ok. so i have a list of country names which have been abbreviated. we'll call this list A i have another list that which contains the what country each abbreviated name means. we'll call this list B. so example of the content of list B: #delimited by tabs #ABBR COUNTRY COUNTRY... (2 Replies)
Discussion started by: SkySmart
2 Replies

9. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies
qmail-popup(8)						      System Manager's Manual						    qmail-popup(8)

NAME
qmail-popup - read a POP username and password SYNOPSIS
qmail-popup hostname subprogram DESCRIPTION
qmail-popup reads a POP username and password from the network. It then runs subprogram. qmail-popup is most commonly invoked from inetd as qmail-popup CHANGEME checkpassword qmail-pop3d Maildir with CHANGEME replaced by the fully qualified domain name of the local host. qmail-popup expects descriptor 0 to read from the network and descriptor 1 to write to the network. It reads a username and password from descriptor 0 in POP's USER-PASS style or APOP style. It invokes subprogram, with the same descriptors 0 and 1; descriptor 2 writing to the network; and descriptor 3 reading the username, a 0 byte, the password, another 0 byte, an APOP timestamp derived from hostname, and a final 0 byte. qmail-popup then waits for subprogram to finish. It prints an error message if subprogram crashes or exits nonzero. qmail-popup should be used only within a secure network. Otherwise an eavesdropper can steal passwords. Even if you use APOP, an active attacker can still take over the connection and wreak havoc. qmail-popup has a 20-minute idle timeout. qmail-popup is based on a program contributed by Russ Nelson. SEE ALSO
maildir(5), qmail-local(8), qmail-pop3d(8) qmail-popup(8)
All times are GMT -4. The time now is 10:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy