Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script to Search

Status
Not open for further replies.

AlStl

MIS
Oct 2, 2006
83
US
I have a script (find_keyword.ksh) that reads data from a txt (keyword.txt) file and produces results if any word in txt files matches in the directory containing files that end with .ksh

I pass directory path at the time of script execution.

I am facing two issue that I would like some tips on. 1) Current script only searches for only upper case. I would like it to read lower case keywords from txt file and search them as well. 2) In the same script I want to search keywords not only in .ksh files, but *.sql.*

I tried, but can’t seem to find how to include item 1 & 2. Any help will be highly appreciated. I am using solaris 10.


Sample: Keyword.txt
NUMBER
AUTO
number
PARTITION

find_keyword.ksh:

#!/bin/ksh

SCRIPTDIR=$1

# Location of the keyword file:
RSVWDS=/name/europa/keyword.txt

cd $SCRIPTDIR

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=toupper($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh

exit

 
If you get rid of the toupper statement, it will process things exactly as written. And you could just duplicate everything from nawk to *.*ksh and change it for *.*sql.

Just a quick and dirty solution. I'm sure there are more eloquent ways.....

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive innovative agile big data clouds)


 
l0=toupper($k); w=0;


==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive innovative agile big data clouds)


 
John, Thanks.

Actually, I wanted to simultaneously search within *.sql.* and *.*ksh files.

I tried to do this, but it did not work.

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=toupper($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh *.sql.*

exit
 
just duplicate the code:

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=toupper($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=toupper($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.sql.*

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive innovative agile big data clouds)


 
okay. That works.

One last thing. If my *.sql.* or *.*ksh file has:

IMD.SATURN
SATURN_4500_a
Select * from IMD.SATURN_4500_a

How can I pick all these 3 lines in my results by just having below in my keyword.txt file:

saturn

Thanks!
 
again, the quick and dirty way is to duplicate the code.

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=toupper($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh

nawk -F'[ ()+-;]' -v words=$RSVWDS '
BEGIN{n=0; while ((getline line < words ) > 0){ n=n+1; rw[n]=line;} close(words);}
{for(k=1;k<=NF;k++){
l0=tolower($k); w=0;
for(i in rw){if(length(l0)>0&&l0==rw) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh

or you could use grep
grep SATURN *.*ksh*
grep saturn *.*ksh*
and if you are not in the directory where the files are located:
grep SATURN /path/to/files/*.*ksh*

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than cognitive innovative agile big data clouds)


 
The problem is that it ignores the lines in a .ksh or .sql file for example:

Select * from IMD.SATURN_4500_a


Because it has IMD. before and _4500_a at the end

I still want it to return above line in search results because it has saturn in it. The idea is to automated the search and result because my keyword.txt could contain large number of words.

 
Regular Expressions

egrep ".*SATURN.*"

use -i to make it case insensitive

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top