kainaw
kainaw

Reputation: 4334

Grep files matching result of grep from a file

I would like to do the following, but I don't seem to be able to wrap my brain around it today...

I have a section number, such as 45. I have a file of employee data that ends with the employee's section number. So, I can grab all the employees with:

grep ",45$" /data/employees.dat

Now, that returns something like the following. I omitted the multiple columns of data for brevity. The first column is the ID. The last is the section number.

38275,...some data...,45
4718573,...some data...,45
328,...some data...,45

Now, on to the hard part... In /data is a set of directories. Each directory is named p#### where #### is an employee number. Inside of those directories is a set of files. I am interested in /data/p####/contacts.csv. I have a key value ("Bob" in this example) and I want to find every line containing the key value in the contacts.csv file for each employee of the previous grep. I do not want to do this by hand because in the real work I will receive a few thousand results from each grep.

grep Bob /data/p38275/contacts.csv
grep Bob /data/p4718573/contacts.csv
grep Bob /data/p328/contacts.csv

It seems to me that I should be able to grep on a pattern for the filename, but then I have to crap the first grep in as the pattern - which I don't think I can do. If the only good solution is to write a script to do this all by hand, I will do that. Right now, I'm messing with sed and awk to see if anything starts to make sense.

Upvotes: 0

Views: 79

Answers (1)

Wintermute
Wintermute

Reputation: 44063

I'd say

grep Bob $(awk -F, '$NF == 45 { print "/data/p" $1 "/contacts.csv" }' /data/employees.txt)

or possibly

grep -h Bob $(awk -F, '$NF == 45 { print "/data/p" $1 "/contacts.csv" }' /data/employees.txt)

if you don't want the filenames where Bob was found as part of the output.

The way it works is that

awk -F, '$NF == 45 { print "/data/p" $1 "/contacts.csv" }' /data/employees.txt

prints the list of filenames (built from the first field and string constants where the last field is 45), which is then passed to grep as parameters (that's what the $() command substitution is for). Note that this expects that there are no whitespaces in the fields, although I can't imagine that there are, given the example input data.

Upvotes: 1

Related Questions