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 01-26-2013, 05:26 AM   #1
troutatious
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
Need help to Find and Replace code in multiple locations


I have php code I want to grep and replace in about a thousand "jgalleryloop.php" files on my website.

Here's the old code.

echo '<li><a href="http://www.postrain4.com/';
echo $folder;
echo '/load/mls/'.$value;
echo '.jpg"><img src="http://www.postrain4.com/';
echo $folder;
echo '/load/smallimage/'.$value;
echo '.jpg" alt="" /></a></li>';

Here is the new code I want to replace it with:

echo'<image>';echo'<big_image_path>http://www.postrain4.com/';echo $folder;echo'/load/bigimage/'.$value;echo'.jpg</big_image_path>';echo'<mobile_image_path>http://www.postrain4.com/';echo $folder;echo'/load/mls/'.$value;echo'.jpg</mobile_image_path>';echo'</image>';

I tried to grep and sed using backslashes on special characters, but I got a syntax error
 
Old 01-27-2013, 12:12 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If both your new and old code files contain only the code that you have mentioned (nothing else), then a simple script can do the job.

(1) Create a new file, and put new code in that:
Code:
~$ vi /home/username/new_code.txt
echo '<li><a href="http://www.postrain4.com/';
        echo $folder;
        echo '/load/mls/'.$value;
        echo '.jpg"><img src="http://www.postrain4.com/';
        echo $folder;
        echo '/load/smallimage/'.$value;
        echo '.jpg" alt="" /></a></li>';
(2) If you don't know the location of all old files i.e. they are all over your system, then first find and save them in a text file for safe side working:
Code:
~$ su - root
~$ find / -name "jgalleryloop.php" -print > /tmp/old_file_list.txt
OR 
~$ find / -name "*.php" -print > /tmp/old_file_list.txt
(3) Check the list of files:
Code:
~$ cat /tmp/old_file_list.txt
(4) Create a script:
Code:
#!/bin/bash
INFILE=/tmp/old_file_list.txt
NEWCODE=/home/username/new_code.txt
while read -r file
do 
cat $NEWCODE > $file
echo "$file updated with new code."
done < $INFILE
Note: Before invoking script, once specify that are all your old files have same name i.e. jgalleryloop.php or those files have just a .php extension?

Last edited by shivaa; 01-27-2013 at 12:15 AM.
 
Old 01-27-2013, 05:38 AM   #3
troutatious
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you!

Shivaa, you are awesome. Thank you. That worked like a charm!
 
Old 01-27-2013, 06:30 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Thanks


If it worked, then mark the thread as solved (on top menu, select Thread Tools option and choose Mark this thread as Solved).
 
  


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
[SOLVED] how we can find and replace multiple file using sed command jayakumar01 Linux - Server 1 04-05-2012 08:53 AM
[SOLVED] Find and replace multiple instances gdavis2287 Programming 23 08-16-2010 01:40 PM
[SOLVED] Find and replace in multiple files in ssh whitelinux Linux - Server 1 07-03-2010 05:43 PM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 09:10 AM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 03:31 AM


All times are GMT -5. The time now is 10:10 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