LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Simple Bash Script to grep for keywords in MySQL dump files (http://www.linuxquestions.org/questions/programming-9/simple-bash-script-to-grep-for-keywords-in-mysql-dump-files-730180/)

eodchop 06-02-2009 11:47 AM

Simple Bash Script to grep for keywords in MySQL dump files
 
Greetings all,

I am looking for a simple example of a bash script to grep for keywords in MySQL dump files. The files are located in different directories is the problem. Anyone have an idea of where to get started?

/path/to/parent/mysql/dump/directory

/server1
/server2
/server3

etc.

MensaWater 06-02-2009 12:08 PM

grep allows you to specify multiple files - since 3 of your directories have only one character difference you can specify it like this:

grep <keyword> /path/to/parent/mysql/dump/directory/* /server?/*

That will include the file name in which it was found in the output.

pgpython 06-02-2009 12:09 PM

try
Code:

for KEYWORD in $*; do
      grep -r $KEYWORD /path/to/parent/mysql/dump
done

then you call it as ./mysqlfind keyword1, keyword2, ...

eodchop 06-02-2009 02:02 PM

Tried this!
 
[QUOTE=pgpython;3560519]try
Code:

for KEYWORD in $*; do
      grep -r $KEYWORD /path/to/parent/mysql/dump
done

I tried

Code:

#!/bin/bash
for KEYWORD in $*; do
      grep -r $http: /path/to/parent/mysql/dump/*
done

i am looking for urls that start with "http:" in the dump files. Not getting any output from this. Also, is there a way i can direct the output of what grep finds to a text file?

Thanks a million,

Newb Sys Admin

MensaWater 06-02-2009 02:58 PM

You're misunderstanding the $KEYWORD. That is a variable set when you invoke the script.

Put the script as written into a file (e.g. grepit.sh), make the file executable (e.g. chmod 755 grepit.sh) then run the file with the keyword you want e.g.
./grepit.sh http:

The file (called a script) will convert set KEYWORD variable as what you typed (httpd: ) so when it greps for $KEYWORD it will actually be grepping for "httpd:"

If you later decide to look for something else it you type that. For example if you want to look for .org you'd type:
./grepit.sh .org

pgpython 06-02-2009 03:16 PM

You can use the > character to redirect your output:

Code:

./grepit.sh httpd .org > /var/log/mylog.log
make sure you have rw permission on tthe file your logging to though

the > character will erase the files previous contents if it exists. If you want to append to the file instead use >>

jlinkels 06-02-2009 06:39 PM

Not sure whether you make the dump especially with the purpose to perfom a text search on it, or the dump file is just what you happen to have.

In case you make the dumpfile especially to perform a text search, why not query the database from bash?

Code:

echo "SELECT urlname, morecolumns FROM tablename WHERE urlname LIKE \"%http://%\"" | /path/to/mysql -s -uusername -ppassword
Note the absene of spaces in -uusername and -ppassword.

jlinkels

tonyadverts 06-05-2009 01:35 AM

Quote:

Originally Posted by eodchop (Post 3560494)
Greetings all,

I am looking for a simple example of a bash script to grep for keywords in MySQL dump files. The files are located in different directories is the problem. Anyone have an idea of where to get started?

/path/to/parent/mysql/dump/directory

/server1
/server2
/server3

etc.

If your
/server1
/server2
/server3

are under
/path/to/parent/mysql/dump/directory


then using grep -r "Search_string" /path/to/parent/mysql/dump/directory should do the trick!
:)

Linux Archive


All times are GMT -5. The time now is 03:57 AM.