Search Many Files and Print

Search for the contents in many file and print that file using shell script.

Have a file1
H87I
Y788O
T347U
J23U

and
file2 J23U U887Y I99U T556U
file3 I99O J99T F557J
file4 N99I T666U R55Y
file5 H87I T347U
file6 H77U R556Y E44T
file7 Y788O K98U H8I

May be using script we can use file1 to search for all the files 
and have the output 
H87I file5
Y788O file7
T347U file5
J23U file2

Solution:

Do this..you should get what you want..

while read line
do
filefound=`grep -l $line file2 file3 file4 file5 file6 file7`
output=`echo $filefound | cut -d: -f1`
echo "$line $output"
done < file1

or

I think this one is ok.

Input:

Code:
a:
H87I
Y788O
T347U
J23U

b:
file2 J23U U887Y I99U T556U
file3 I99O J99T F557J
file4 N99I T666U R55Y
file5 H87I T347U
file6 H77U R556Y E44T
file7 Y788O K98U H8I

Code:

awk '
{
if (NF==1)
 a[$1]=$1
else
 for (i in a)
  if($2==a[i])
   a[i]=$1
}
END{
for (i in a)
print i " "a[i]
}' a b

Output:

Code:

H87I file5
J23U file2
T347U T347U
Y788O file7

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.