Bash script (using find and grep)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script (using find and grep)
# 1  
Old 11-14-2009
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  
Old 11-14-2009
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  
Old 11-15-2009
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  
Old 11-15-2009
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  
Old 11-15-2009
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  
Old 11-15-2009
Code:

SEARCHQUERY="searchword1.*searchword2.*searchword3"

# 7  
Old 11-15-2009
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use grep in a loop using a bash script?

Dear all, Please help with the following. I have a file, let's call it data.txt, that has 3 columns and approx 700,000 lines, and looks like this: rs1234 A C rs1236 T G rs2345 G T Please use code tags as required by forum rules! I have a second file, called reference.txt,... (1 Reply)
Discussion started by: aberg
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

3. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

4. Shell Programming and Scripting

Problem using grep in bash script

When it comes to programing and UNIX, I know just enough to be really really dangerous. I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
Discussion started by: errcricket
4 Replies

5. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

6. Shell Programming and Scripting

Bash script - Grep

Hi, I am very new to bash scripting and I need to write a bash script that takes two arguments, a string and a file. The output should be each line which matches the string *from the beginning of the line*. For example, given a string "ANA" the line starting with "ANABEL" will be printed, but... (9 Replies)
Discussion started by: jboy
9 Replies

7. Shell Programming and Scripting

Bash Script to Find the status of URL

#!/bin/bash timevar=`date +%d-%m-%Y_%H.%M.%S` #-- > Storing Date and Time in a Variable get_contents=`cat urls.txt` #-- > Getting content of website from file. Note the file should not contain any http:// as its already been taken care of echo "Datae-time URL Status code Report" >... (2 Replies)
Discussion started by: anishkumarv
2 Replies

8. Shell Programming and Scripting

indication of find activity - bash script

Hi All, I wanted to show on stdout that a file was found right after it happens due to indicate the activity of long search. Further more I want to store the result of the find in a file. I have tried this: echo -n "Searching" find . -name Makefile -type f -print -exec echo -n "." \; >... (16 Replies)
Discussion started by: vercsab
16 Replies

9. Shell Programming and Scripting

Find out the day in Bash Shell script

Hello All, I need a bash shell script to find out a day from the date.For example we give the date(20100227/YYYYMMDD) then we get the day 'Saturday'. Thanks in advance, Satheesh (5 Replies)
Discussion started by: satheesh4093
5 Replies

10. Shell Programming and Scripting

Script to find, grep and email

running a suse linux 10. I'm trying to write a script that searches for a file with certain name and that has been modified less than 30 minutes ago, then search for a certain string in that file and email this string out. I have tried different combinations of find, grep and email but no luck... (7 Replies)
Discussion started by: basisvasis
7 Replies
Login or Register to Ask a Question