• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

script help, finding phrases in a file

sciencewhiz

Diamond Member
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.
 
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.
 
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
 
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;
 
Back
Top