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:
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
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 ?
For starters, I'd rewrite the script you posted. The grep isn't needed, and since you're looking for a fixed set of files (*.php I assume), and fixed replace criteria, you don't need to worry about command line parameters. (You should if this is more than a one-off script, but I'll assume you don't need a script that you can run with different criteria.)
The basic script:
The tricky question is what criteria you need to determine which lines of the files to delete. Depending on what it is, the 'insert sed here' line in the previous script will need to change to do the right thing.
If your block of code is bounded by a unique string or phrase in the first and last lines then it will be a simple sed. The more complicated the criteria, the more complicated the code will need to be. Bottom line: post your criteria for deletion and someone will give you some help with writing a sed/awk or something that will delete things.
As for creating a script...
Start your favorite text editor, insert the code from above, or other of your choice, and save the file. Then at the command line, enter this command (assumes the file you saved was called delete_frm_php.ksh):
You then can invoke your script from the command line by typing delete_frm_php.ksh
Is the block of code the only block that starts <?php and finishes ?>? I suspect that maybe there are other blocks that start and end this way, but on the off chance that this will be the only block like this, then this sed should work:
It deletes all lines between the starting line with "<?php" and the ending "?>" line as it reads the file. The updated file is written to $file.
If you can use this sed, just replace it in the earlier example.
If there are more than one php blocks of code, then you'll need to find a unique string inside the block that you want to delete. Change the one line in the script below that has "/enter your nickname/" to contain the unique string from the block of code and it should find and delete the lines containing the string.
Hope this helps get you going.
Last edited by agama; 03-04-2012 at 01:44 PM..
Reason: corrected comment that introduced a bug
No such file or directory cd: /directory/path/where/you/want/to/start (i did replaced this with the full path of the directory i wanted it to start)
If you cut and paste the cd command with your path at the command line do you get the same error? Screams typo in the path to me, but it is hard to say from here.
Quote:
BTW, when i execute it just by typing
it says :
You'll need to make the script executable. You can change the file's mode to turn on the executable bit with the chmod command. This should work:
If you've done that then I'm a bit confused. And if you want bash to execute the script, rather than ksh, then change the ksh reference in the first line to bash.
Quote:
when i execute it typing
it says:
I also a few tweaks in the path, no success
Any clues ? Thank you!
Without seeing the changes you've made this is impossible to give any suggestions for. Sounds like you've got a missing quote (check out line 32 to start with). You can post the whole script if you cannot find the missing quote.
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)
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)
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)
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)
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)
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)
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)
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)
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)