LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-02-2009, 11:47 AM   #1
eodchop
LQ Newbie
 
Registered: Nov 2008
Posts: 14

Rep: Reputation: 0
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.
 
Old 06-02-2009, 12:08 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,934
Blog Entries: 5

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
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.
 
Old 06-02-2009, 12:09 PM   #3
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 141

Rep: Reputation: 32
try
Code:
for KEYWORD in $*; do
      grep -r $KEYWORD /path/to/parent/mysql/dump 
done
then you call it as ./mysqlfind keyword1, keyword2, ...
 
Old 06-02-2009, 02:02 PM   #4
eodchop
LQ Newbie
 
Registered: Nov 2008
Posts: 14

Original Poster
Rep: Reputation: 0
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

Last edited by eodchop; 06-02-2009 at 02:04 PM. Reason: missing code snippet
 
Old 06-02-2009, 02:58 PM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,934
Blog Entries: 5

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
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

Last edited by MensaWater; 06-03-2009 at 07:58 AM.
 
Old 06-02-2009, 03:16 PM   #6
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 141

Rep: Reputation: 32
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 >>
 
Old 06-02-2009, 06:39 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 06-05-2009, 01:35 AM   #8
tonyadverts
Member
 
Registered: Jun 2009
Posts: 46

Rep: Reputation: 16
Quote:
Originally Posted by eodchop View Post
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

Last edited by tonyadverts; 06-18-2009 at 01:50 AM.
 
  


Reply

Tags
bash, grep, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to list files, drop extension and dump to file talwar_ Programming 10 06-03-2011 09:18 AM
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 12:17 PM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 05:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
bash script and grep syros Programming 4 01-13-2005 03:04 PM


All times are GMT -5. The time now is 09:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration