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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-02-2009, 12:47 PM
|
#1
|
LQ Newbie
Registered: Nov 2008
Posts: 14
Rep:
|
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.
|
|
|
06-02-2009, 01:08 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
|
|
06-02-2009, 01:09 PM
|
#3
|
Member
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142
Rep:
|
try
Code:
for KEYWORD in $*; do
grep -r $KEYWORD /path/to/parent/mysql/dump
done
then you call it as ./mysqlfind keyword1, keyword2, ...
|
|
|
06-02-2009, 03:02 PM
|
#4
|
LQ Newbie
Registered: Nov 2008
Posts: 14
Original Poster
Rep:
|
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
|
|
|
06-02-2009, 03:58 PM
|
#5
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
|
|
06-02-2009, 04:16 PM
|
#6
|
Member
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142
Rep:
|
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 >>
|
|
|
06-02-2009, 07:39 PM
|
#7
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
|
06-05-2009, 02:35 AM
|
#8
|
Member
Registered: Jun 2009
Posts: 46
Rep:
|
Quote:
Originally Posted by eodchop
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.
|
|
|
All times are GMT -5. The time now is 01:15 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|