select a record from one file matching from second file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select a record from one file matching from second file using awk
# 1  
Old 06-05-2008
select a record from one file matching from second file using awk

I need help Smilie

I have two input files and I'd like to generate a report based on the two.

filea:
hostname,account1,password
,account2,password
hostname,account1,password
hostname,account1,password
,account1,password
,account2,password

repeating hostnames are blank

fileb:
hosta
hostb
hostc

I need to generate a record of "hostname account password" for all hosts in fileb that occur in filea for a specific account. I.e. I want a list of root passwords for all hosts in fileb. I know the logic I want to use but don't have the scripting skills to implement it. I think the easiest way would be to use awk to fill in the blanks with the hostname of filea and then simply use a shell script to grep for each host in fileb of the new filea.

The other way is to run down filea for each hostname in fileb until either the account is matched or the hostname is different and print the result. If the account does not exist for the given hostname do nothing.

I'm trying to save manually doing this by scripting it thinking it would be faster but maybe I was wrong Smilie

Here is the framework I came up with so far for the harder way because it would be more elegant Smilie

#!/bin/sh
#
hosts=`cat hosts.txt`
hostname="x"
last="x"

for name in ${hosts}
do
#the awk logic would come here
done
# 2  
Old 06-05-2008
you may give us an example of the result that you try to achieve and base on that we may come up with a solution.
# 3  
Old 06-06-2008
You mean something like this?

Code:
sed -e 's/.*/^&,/' fileb | grep -f - filea

Not all grep versions support the -f option apparently, but the idea is simply to grep for anything from fileb which is in the first field of filea.
# 4  
Old 06-06-2008
Quote:
Originally Posted by era
You mean something like this?

Code:
sed -e 's/.*/^&,/' fileb | grep -f - filea

Not all grep versions support the -f option apparently, but the idea is simply to grep for anything from fileb which is in the first field of filea.
Thanks for the reply. My version does not support -f unfortunately. Looking at the code where would I specify the account name I'm looking for? Say if I want to list the host,account,password for all root accounts? Also would this work for records where the host name is blank?

To clarify the problem, filea has blanks for repeating hostnames and if say root is in a record where there is no hostname how do you derive the hostname for the record? The hostname I'm looking for is the prior non blank record.

example:

hosta,johndoe,abc123
,root,4009dlkj
hostb,janedoe,rrrrrr

if host a is in fileb the expected out put for root is

hosta,root,4009dlkj

I was trying to use awk to replace the blank with a hostname from the previous nonblank record but I kept getting errors. I couldn't even store $1 to a variable and print it as a test. I know sed is very powerful but I didn't think this could be done with one line.

I ended up doing it manually last night. At this point it's inquiring minds want to know and it might come in handy if I have to do it again.

TIA
# 5  
Old 06-06-2008
Quote:
Originally Posted by era
Not all grep versions support the -f option apparently, but the idea is simply to grep for anything from fileb which is in the first field of filea.
True, but most that don't will support either egrep -f or fgrep.
# 6  
Old 06-09-2008
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.)
# 7  
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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