script help, finding phrases in a file

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
I have a line delimited file of words/phrases and I think at least one of those phrases is contained in another file, but need to verify. I did a quick check and didn't see anything obvious. However, if one of the words in the file is contained in a longer word in the other file, I need to find it also. For example, if ass is one of the words, I need to also find passionate.

I'm sure there's a million ways to do this.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
Using a BASH script should be fine for that. Unless there is some language you already know, BASH would be the simplest and quickest to learn.

Here is a guide that I, as a BASH beginner, love to use: http://tldp.org/LDP/abs/html/

You'll want to use a "for" loop. I'm not sure on the exact syntax, but it would be something like:

for phrase in $( read each line of a file ); do
grep phrase file_name.txt
done

I don't know what the command would be for reading in a line of a file, though. Probably use "cat"?

for phrase in $( `cat source_file.txt` ); do
grep phrase file_name.txt
done

And notice those are backticks around "cat source_file.txt", not quotes.
 

crontab

Member
Dec 20, 2000
160
0
0
I am assuming that list.txt has your list of terms you want to look for and file.txt is the file you want to search in.

If you want to use a for loop do

for i in `cat list.txt`
do
grep $i file.txt
done

For a shell one liner use

cat list.txt | xargs -i grep {} file.txt
 

DarkThinker

Platinum Member
Mar 17, 2007
2,822
0
0
while read line; do
#foo2.txt is the file containing possible matches
cat foo2.txt| grep ${line}

done < foo.txt #The file the loop reads lines from
exit;