ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped.
First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes.

I have a text file (file_source) of terms, each line could have one, two or more words in this file.
I want to use each line from file_source as a key to grep for in a different file, (file_target)
I want to run a script that reads file_source, and uses the words from each line, to search/grep in file_target.
IE: grep "one first word" file_target, or just grep "second" file_target, etc.

In my first attempted script I have run a=`cat file_source`, then in a do loop, tried to grep "$a" file_another.
But instead of looking for all the words in one line, the routine I've created looks for each word individually from the line it's reading.
So instead of grep "a b c" file, I got grep a file, grep b file, grep c file.
Which is great... but sometimes, I really need to know that a, b and c occurred as a phrase.

I've tried multiple ideas, from cat, to read, to while to screaming. (OK, screaming is not a function, just a reaction.)
I can get a while read routine to read the whole line of file_source, but then trying to use the line to grep for in file_target fails wonderfully, saying something to the effect of source not found or what not.

I've run the script in the directory of the file I want to look at, calling out the file, and also run it pointing to the full path/file, to no avail.

If anyone has quick suggestions, outlines, ideas, or examples, WITHOUT burning up too much of your time, I'd appreciate it.
Thank you.

Bruce

You had the example

grep "one first word" file_target

This is different from

grep one first word file_target

where it searches the word "one" in the files "first", "word", "file_target".
So ensure you have the quotes, even if you have a variable line from a while read line

grep "$line" file_target
1 Like

This is not advisable for several reasons: first - as you noticed - field splitting will occur and may bite you in the behind. Second, there is a much easier way to do this, see below.

Not seeing your script i can only speculate but probably the problem was quoting (or, rather, the lack thereof). You probably did:

while read line ; do
     grep $line /some/file
done < /some/file_with_phrases

Notice that you always ALWAYS have to protect your variables - you don't go outside naked, they shouldn't be made to go outside unquoted. Do it like:

while read line ; do
     grep "$line" /some/file
done < /some/file_with_phrases

But there is a much better way and it doesn't even involve a script - TADAAAAHHHH:

Well, sometimes, when the time is right, i can make a little room in my busy schedule which is filled with coming up with witty formulations for heightening the suspense and avoiding to lead to a premature climax of this most interesting topic ......*)

...to come up with a single command that does it all: grep !

Do it like this:

grep -f /some/file_with/phrases /file/to/grep/in

and grep will read the file_with_phrases, line by line, then do a search in the other file for that line. Sounds like this is what you wanted, no? To find out more about how to use grep i suggest to peruse the man page. If you still have questions, you'll be welcome.

I hope this helps.

bakunin
__________
*) You might not believe it but i can prolong that for a nearly indefinite time.

I was trying to be careful... but will double check MadeInGermay.... Thanks.

------ Post updated at 03:36 PM ------

hey Bakunin...
A - Are you sure I don't run around out there nak.... oh never mind.
And yes, it worked... the suspense got me.

ARE YOU KIDDING ME? Crap on a crapsicle stick! Grep -f....

Thank you for your time, oh wise and humorous one.

Bruce