Korn Shell Script - Read File & Search On Values

I am attempting to itterate through a file that has multiple lines and for each one read the entire line and use the value then to search in other files. The problem is that instead of an entire line I am getting each word in the file set as the value I am searching for. For example in File 1 instead of searching for "The UNIX World" my program searches for "The" and then "UNIX" and then "World".

File 1

The UNIX World
Words
Retro data is here somewhere
Quit it right now123

What I wrote was the following:

set -A Value_List `cat $1`

for item in ${Value_List[*]}
do
FOUNDIT=`grep $item $2`
if [ ! -z ${FOUNDIT} ]; then
echo "Not Found $item"
else
echo $item >> item.results
fi
done

Can someone help me get the rest of the way there?


while read item 
do
  FOUNDIT=`grep "$item" $2` 
  if [ ! -z ${FOUNDIT} ]; then
    echo "Not Found $item"
  else
    echo $item >> item.results
  fi
done < $1

or more simply

fgrep -f $1 $2 > item.results

1.) instead of reading the content of your search keys into an array in advance you should read this file line by line in a pipeline. Arrays are limited in size and you may run into a situation where the number of search keys exhaust the maximum number of available array elements. You will have to decide for yourself how big the chance of this happening is in your case.

2.) Don't use the old-style subshell mechanism any more. Instead of "x=`command`" use "x=$(command)"

3.) The reason why a search key consisting of several words is not being searched in its entirety but only one word after the other is because you haven't protected your variables enough. The shell tends to interpret values and especially splits words at word boundaries (read: whitespace). To prevent the shell from doing so is the whole point of quoting. This makes the difference between 'x=$y' and 'x="$y"'. You can easily test this by issuing the following series of statements at the commandline:

# touch "foo bar"
# ls -1
# foo bar
# touch foo bar
# ls -1
foo bar
foo
bar
# ls -1 foo bar
foo
bar
# ls -1 "foo bar"
foo bar

Having said that, the script would look like the following sketch (note the quotes in the line starting with grep):

#!/bin/ksh

typeset fKey="/my/search/key/file"
typeset chKey=""
typeset fSearchList="/path/to/all/files/to/search"
typeset fSearch=""
typeset fResult="/path/to/result/file"

cat "$fKey" | while read chKey ; do
     ls -1 "$fSearchList" | while read fSearch ; do
          grep "$chKey" "$fSearch" >> "$fResult"
     done
done

exit 0

Hope this helps.

bakunin