Operating System - HP-UX
1844205 Members
2531 Online
110229 Solutions
New Discussion

Matching text from files within same Tree

 
Burney
New Member

Matching text from files within same Tree

Hi,
I need my script to look for same text within the specified files. could be multiple files in a single directory.

user should not specify the text.Only the files would be specified.So the script should search for each line of text which it picked from the first file and then look for the same text in other files. Should also give how many times that same text was found e.g

text1.txt has data

Mark
Anthony

script would look for Mark first in all the other files and then give result of hits from other files.Likewise same for anthony.
4 REPLIES 4
Mel Burslan
Honored Contributor

Re: Matching text from files within same Tree

quick and dirty script with filenames hardcoded, but you can replace them with command line arguments.

searchlist="file1 file2 file3"
cat text1.txt | while read line
do
for f in $searchlist
do
count=`grep "$line" $f | wc -l`
if [ $count -gt 0 ]
then
echo "$line was found in $f, $count times"
else
echo "$line was not found in any other files listed"
fi
done
done


please take this segment of code with a grain of salt as it is not tested anywhere.
________________________________
UNIX because I majored in cryptology...
RAC_1
Honored Contributor

Re: Matching text from files within same Tree

cd /where file_reside

for i in `cat first_file`
do
echo "Finding $i"
grep $i * > /tmp/${i}.find
done

Anil
There is no substitute to HARDWORK
Marvin Strong
Honored Contributor

Re: Matching text from files within same Tree


well not sure this is will help

grep pattern | wc -l



Rodney Hills
Honored Contributor

Re: Matching text from files within same Tree

If you are looking for which text appears in which files and each record is the text you are looking, then a perl script might be the answer-

#!/usr/bin/perl
opendir(DIR,".");
@files=readdir(DIR);
closedir(DIR);
foreach $file (@files) {
open(INP,"<$file");
while() {
chomp;
$hold{$_}{$file}++;
}
close(INP);
}
foreach $text (sort keys %hold) {
@filelist=keys %{$hold{$text}};
print "$text appears\n";
foreach $file (@filelist) {
printf " %8d %s\n",$hold{$text}{$file},$file;
}
}

Would produce a list of

Mark appears
1 text1.txt
4 text2.txt
...
Anthony appears
1 text1.txt
3 text9.txt
...
...

HTH

-- Rod Hills
There be dragons...