Sponsored Content
Top Forums Shell Programming and Scripting Script to find & replace a multiple lines string across multiple php files and subdirectories Post 302604136 by spfc_dmt on Saturday 3rd of March 2012 09:06:49 AM
Old 03-03-2012
Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ?

The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories.

So lets say im at root/dir1/dir2/ , when i execute the script it should search and replace a piece of code on php files inside this directory and its subdirectories.

The closest i found i think it was this:

Code:
Paste this into a file ‘renall’ and make it executable (chmod u+x renall): #!/bin/sh
 if [ $# -lt 3 ] ; then
   echo -e “Wrong number of parameters.”
   echo -e “Usage:”
   echo -e ”  renall ‘filepat’ findstring replacestring\n”
   exit 1
fi
 #echo $1 $2 $3
for i in `find . -name “$1″ -exec grep -l “$2″ {} \;`
do
mv “$i” “$i.sedsave”
sed “s/$2/$3/g” “$i.sedsave” > “$i”
echo $i
#rm “$i.sedsave”
done

But the string i need to find and replace have multiple lines, so i dont know how to put it in this script.

Also i dont know how to save and execute this script Smilie

EDIT: AH and by the way, i need to only DELETE the piece of code from these PHP files, i dont need to replace it with something else...

Could anyone please give me detailed instructions on this ?

Thanks in advance!

Last edited by spfc_dmt; 03-03-2012 at 10:13 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies

2. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies

3. Shell Programming and Scripting

replace multiple lines in multiple files

i have to search a string and replace with multiple lines. example Input echo 'sample text' echo 'college days' output echo 'sample text' echo 'information on students' echo 'emp number' echo 'holidays' i have to search a word college and replace the multiple lines i have... (1 Reply)
Discussion started by: unihp1
1 Replies

4. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

5. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

6. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

7. Shell Programming and Scripting

how can i find number of lines in files & subdirectories

how can i find number of lines in files & subdirectories ? (3 Replies)
Discussion started by: pcbuilder
3 Replies

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

10. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies
POPEN(3)								 1								  POPEN(3)

popen - Opens process file pointer

SYNOPSIS
resource popen (string $command, string $mode) DESCRIPTION
Opens a pipe to a process executed by forking the command given by $command. PARAMETERS
o $command - The command o $mode - The mode RETURN VALUES
Returns a file pointer identical to that returned by fopen(3), except that it is unidirectional (may only be used for reading or writing) and must be closed with pclose(3). This pointer may be used with fgets(3), fgetss(3), and fwrite(3). When the mode is 'r', the returned file pointer equals to the STDOUT of the command, when the mode is 'w', the returned file pointer equals to the STDIN of the command. If an error occurs, returns FALSE. EXAMPLES
Example #1 popen(3) example <?php $handle = popen("/bin/ls", "r"); ?> If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell: Example #2 popen(3) example <?php error_reporting(E_ALL); /* Add redirection so we can get stderr. */ $handle = popen('/path/to/executable 2>&1', 'r'); echo "'$handle'; " . gettype($handle) . " "; $read = fread($handle, 2096); echo $read; pclose($handle); ?> NOTES
Note If you're looking for bi-directional support (two-way), use proc_open(3). Note When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable. Warning With safe mode enabled, the command string is escaped with escapeshellcmd(3). Thus, echo y | echo x becomes echo y | echo x. SEE ALSO
pclose(3), fopen(3), proc_open(3). PHP Documentation Group POPEN(3)
All times are GMT -4. The time now is 10:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy