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
SUBSTR_REPLACE(3)							 1							 SUBSTR_REPLACE(3)

substr_replace - Replace text within a portion of a string

SYNOPSIS
mixed substr_replace (mixed $string, mixed $replacement, mixed $start, [mixed $length]) DESCRIPTION
substr_replace(3) replaces a copy of $string delimited by the $start and (optionally) $length parameters with the string given in $replace- ment. PARAMETERS
o $string - The input string. An array of strings can be provided, in which case the replacements will occur on each string in turn. In this case, the $replacement, $start and $length parameters may be provided either as scalar values to be applied to each input string in turn, or as arrays, in which case the corresponding array element will be used for each input string. o $replacement - The replacement string. o $start - If $start is positive, the replacing will begin at the $start'th offset into $string. If $start is negative, the replacing will begin at the $start'th character from the end of $string. o $length - If given and is positive, it represents the length of the portion of $string which is to be replaced. If it is negative, it rep- resents the number of characters from the end of $string at which to stop replacing. If it is not given, then it will default to strlen( $string ); i.e. end the replacing at the end of $string. Of course, if $length is zero then this function will have the effect of inserting $replacement into $string at the given $start offset. RETURN VALUES
The result string is returned. If $string is an array then array is returned. EXAMPLES
Example #1 Simple substr_replace(3) examples <?php $var = 'ABCDEFGH:/MNRPQR/'; echo "Original: $var<hr /> "; /* These two examples replace all of $var with 'bob'. */ echo substr_replace($var, 'bob', 0) . "<br /> "; echo substr_replace($var, 'bob', 0, strlen($var)) . "<br /> "; /* Insert 'bob' right at the beginning of $var. */ echo substr_replace($var, 'bob', 0, 0) . "<br /> "; /* These next two replace 'MNRPQR' in $var with 'bob'. */ echo substr_replace($var, 'bob', 10, -1) . "<br /> "; echo substr_replace($var, 'bob', -7, -1) . "<br /> "; /* Delete 'MNRPQR' from $var. */ echo substr_replace($var, '', 10, -1) . "<br /> "; ?> Example #2 Using substr_replace(3) to replace multiple strings at once <?php $input = array('A: XXX', 'B: XXX', 'C: XXX'); // A simple case: replace XXX in each string with YYY. echo implode('; ', substr_replace($input, 'YYY', 3, 3))." "; // A more complicated case where each replacement is different. $replace = array('AAA', 'BBB', 'CCC'); echo implode('; ', substr_replace($input, $replace, 3, 3))." "; // Replace a different number of characters each time. $length = array(1, 2, 3); echo implode('; ', substr_replace($input, $replace, 3, $length))." "; ?> The above example will output: A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC NOTES
Note This function is binary-safe. SEE ALSO
str_replace(3), substr(3), String access and modification by character. PHP Documentation Group SUBSTR_REPLACE(3)
All times are GMT -4. The time now is 07:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy