LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-10-2014, 12:36 AM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 699

Rep: Reputation: 47
Find + Replace from command line


Hey LQ,

I've got to update some PHP scripts to use the new mysqli extension. I've done a few files manually with find and replace that my IDE has but I'm think there is probably a very simple and very quick shell command that could do this for me.

Basically the update requires any time "mysql" appears in a script, it needs to be switched with "mysqli". Is there an easy way I can just aim a command at an entire directory and have it find and replace all of this?

Thanks so much.
 
Old 12-10-2014, 01:44 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
For the replacement, you may want to use sed. There's a tutorial here. You'll probably also want to use find to generate the list of files and run sed on each one (take a look at find's -exec option).

Last edited by ndc85430; 12-10-2014 at 01:55 AM.
 
1 members found this post helpful.
Old 12-10-2014, 01:58 AM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
Use "sed":

Code:
sed '/s/pattern_to_find/Pattern_to_replace/g' filename

Please note above would not change anything but display the change unless '-i' switch is provided, you can retain the original file by creating backup using "-i.backup" this will create a backup copy of the file in the same directory with suffix .backup
check man sed for more.
 
1 members found this post helpful.
Old 12-10-2014, 07:13 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,550

Rep: Reputation: 137Reputation: 137
Keep in mind that if you use functions like

mysql_query

You may not want to replace that with mysqli_query as this function does not exist...
 
Old 12-10-2014, 06:41 PM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 699

Original Poster
Rep: Reputation: 47
Quote:
Originally Posted by j-ray View Post
Keep in mind that if you use functions like

mysql_query

You may not want to replace that with mysqli_query as this function does not exist...
http://php.net/manual/en/mysqli.query.php

For some reason mysqli_ functions do not appear on the php.net site wide search. But if you search up mysql_query it clearly says it's deprecated and then shows links to the new class.

---------- Post added 12-10-14 at 11:41 PM ----------

Quote:
Originally Posted by SAbhi View Post
Use "sed":

Code:
sed '/s/pattern_to_find/Pattern_to_replace/g' filename

Please note above would not change anything but display the change unless '-i' switch is provided, you can retain the original file by creating backup using "-i.backup" this will create a backup copy of the file in the same directory with suffix .backup
check man sed for more.
Incredible. Thank you.
 
Old 12-10-2014, 08:17 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,468

Rep: Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618
Quote:
Originally Posted by j-ray View Post
Keep in mind that if you use functions like

mysql_query

You may not want to replace that with mysqli_query as this function does not exist...
Just use \bmysql\b as the search string this will match on word boundary's and so ignore msql_query
 
  


Reply


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
How to find and replace where the find is on two lines and replace on first line andrew777 Linux - General 9 08-05-2013 02:50 PM
search folders, find a line in a file, and replace it with a new line ithirdeye Programming 2 04-07-2012 05:27 PM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 06:42 PM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 12:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration