Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match and Select 2

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have two files, one containing a key value (unique values, one per line) and the other containing records eacho of which contains one of the key values. There may not be any records in the second file for a particular key value and there could also be multiples. Key value file has been sorted and run through uniq so there is one and only one of each value in the file.

What I need to do is read each value in the key file one at a time and use it to grep out all records containing that key value from the second file. Those selected records will then be sorted and the last record will be copied to a working file for further processing.

I know how to all of it except getting the key value file opened and each value into a working variable in the .ksh script. The grep, sort, tail, etc. aren't an issue - but darned if I can read each value, one by one, from the input file.

Appreciate any comments.

Tnx as always.

Tom

"My mind is like a steel whatchamacallit ...
 
Code:
for key in $(</path/to/keyfile)
do
 grep "${key}" /path/to/datafile
done

or if there is whitespace in the keys:
Code:
while read key
do
 grep "${key}" /path/to/datafile
done </path/to/keyfile


HTH,

p5wizard
 
If your version of grep has the -f option you could try out this one-liner.

grep -f /path/to/keyfile /path/to/datafile

Cheers,
ND [smile]
 
Thanks to both of you - I think that does the trick!

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top