The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:21 AM
korn shell script to keep history of same file watson2000 UNIX for Dummies Questions & Answers 2 10-09-2006 10: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 06:53 AM
Read file - korn shell alienET Shell Programming and Scripting 5 04-06-2005 02:22 PM
Read a file and replace a value- Bourne and Korn shell kenix Shell Programming and Scripting 3 09-20-2002 09:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-14-2005
run_unx_novice run_unx_novice is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 1
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?
  #2 (permalink)  
Old 06-14-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
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
  #3 (permalink)  
Old 06-15-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0