LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 12:47 PM   #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, 01:08 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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, 01:09 PM   #3
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

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, 03: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 03:04 PM. Reason: missing code snippet
 
Old 06-02-2009, 03:58 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 08:58 AM.
 
Old 06-02-2009, 04:16 PM   #6
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

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, 07:39 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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, 02: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 02: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 10:18 AM
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 01:17 PM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 06:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
bash script and grep syros Programming 4 01-13-2005 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration