Korn Shell Script - Read File & Search On Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell Script - Read File & Search On Values
# 1  
Old 06-14-2005
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  
Old 06-14-2005
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  
Old 06-15-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

2. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

3. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

4. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

7. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

8. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

9. Shell Programming and Scripting

Read file - korn shell

Hello!! I'm want to read each line of a file. I'm doing a cut command, using \n - linefeed as delimeter and "list" as my file. cut -f1 -d"\n" -s < list Changing the file, putting spaces and doing cut -f1 -d" " -s < list it works, but with linefeed nothing is returned. Thanks... (5 Replies)
Discussion started by: alienET
5 Replies

10. Shell Programming and Scripting

Read a file and replace a value- Bourne and Korn shell

Hello, I have a file called Delete and within the delete file I have the following statement: delete from employee where employee_id = " " how do I write a script that read from this file and replace: employee_id = " " with employee_id is null Please assist...greatly... (3 Replies)
Discussion started by: kenix
3 Replies
Login or Register to Ask a Question