The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2009
Posts: 29
Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n).

Code:
#!/bin/bash

SEARCHQUERY="searchword1 searchword2 searchword3";

for WORD in $SEARCHQUERY
do
    GREPINPUT=$GREPINPUT" | grep --ignore-case --files-with-matches -e '$WORD'";
done

FINDFILES=$(find . -maxdepth 2 -name \*.c -type f $($GREPINPUT));

  #2 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2009
Posts: 18
I usually search inside a file with:

Code:
cd /home/users/; grep -R "keyword" * | gawk -F: '{ print $1 }' >> output.txt

This will output the path of the found files to output.txt

Play around with that in your script, should be fun!
  #3 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2009
Posts: 29
OK, but I am trying to insert a concatenated string inside another string and evaluate it. I have been playing with this for hours but cannot get it right.

Any help please?
  #4 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,259
How about:

Code:
#!/bin/bash
SEARCHQUERY="searchword1|searchword2|searchword3"
FINDFILES=$(find . -maxdepth 2 -name \*.c -type f |xargs egrep -lie "$SEARCHQUERY")

-or-

Code:
FINDFILES=$(find . -maxdepth 2 -name \*.c -type f -exec egrep -lie "$SEARCHQUERY" {} \; )

  #5 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2009
Posts: 29
That will output searches that match each keyword. But what I am trying to do is match files that match ALL the keywords.

I'm still playing around with no results.
  #6 (permalink)  
Old 11-15-2009
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,611

Code:

SEARCHQUERY="searchword1.*searchword2.*searchword3"

  #7 (permalink)  
Old 11-15-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,259
An AND function, but not on the same line and not necessarily in that order? OK, I think this might work:

Code:
#!/bin/bash
Q1="searchword1"; Q2="searchword2"; Q3="searchword3";
FINDFILES=$(find . -maxdepth 2 -name \*.c -type f |xargs grep -li "$Q1"|xargs grep -li "$Q2"|xargs grep -li "$Q3")

So you were close but you need xargs to make grep in the file content rather than the file name.

Last edited by Scrutinizer; 11-15-2009 at 12:37 PM..
Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make your bash script run on a machine with csh and bash npatwardhan Shell Programming and Scripting 3 11-19-2008 04:17 AM
Script to find, grep and email basisvasis Shell Programming and Scripting 7 09-21-2008 08:03 PM
Speeding up a Shell Script (find, grep and a for loop) Dave Stockdale UNIX for Dummies Questions & Answers 8 08-11-2008 05:36 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
command find returned bash: /usr/bin/find: Argument list too long yacsil Shell Programming and Scripting 1 12-15-2003 06:38 PM



All times are GMT -4. The time now is 02:58 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