![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 08: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
Code:
fgrep -f $1 $2 > item.results |
|
#3
|
|||
|
|||
|
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
bakunin |
|||
| Google The UNIX and Linux Forums |