The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read values that are passed to the shell function in ksh. prashant43 Shell Programming and Scripting 2 01-22-2008 07:21 AM
korn shell script to keep history of same file watson2000 UNIX for Dummies Questions & Answers 2 10-09-2006 07:38 PM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 03:53 AM
Read file - korn shell alienET Shell Programming and Scripting 5 04-06-2005 11:22 AM
Read a file and replace a value- Bourne and Korn shell kenix Shell Programming and Scripting 3 09-20-2002 06:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-14-2005
Registered User
 

Join Date: Jun 2005
Posts: 1
Stumble this Post!
Question 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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-14-2005
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
Code:
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
Code:
fgrep -f $1 $2 > item.results
Reply With Quote
  #3 (permalink)  
Old 06-15-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,093
Stumble this Post!
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):

Code:
#!/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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0