INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Shell Script to Search

Shell Script to Search

(OP)
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[i]) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh

exit

RE: Shell Script to Search

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)


RE: Shell Script to Search

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

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


RE: Shell Script to Search

(OP)
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[i]) w=w+1;}}
if(w>0){print FILENAME": "FNR" : "$0}
}' *.*ksh *.sql.*

exit

RE: Shell Script to Search

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[i]) 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[i]) 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)


RE: Shell Script to Search

(OP)
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!

RE: Shell Script to Search

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[i]) 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[i]) 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)


RE: Shell Script to Search

(OP)
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.

RE: Shell Script to Search

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close