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 ?
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)
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)
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)
... (11 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)
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)
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)
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)
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
LEARN ABOUT PHP
getopt
GETOPT(3) 1 GETOPT(3)getopt - Gets options from the command line argument listSYNOPSIS
array getopt (string $options, [array $longopts])
DESCRIPTION
Parses options passed to the script.
PARAMETERS
o $options
- Each character in this string will be used as option characters and matched against options passed to the script starting with a
single hyphen ( -). For example, an option string "x" recognizes an option -x. Only a-z, A-Z and 0-9 are allowed.
o $longopts
- An array of options. Each element in this array will be used as option strings and matched against options passed to the script
starting with two hyphens ( --). For example, an longopts element "opt" recognizes an option --opt.
The $options parameter may contain the following elements:
oIndividual characters (do not accept values)
oCharacters followed by a colon (parameter requires value)
oCharacters followed by two colons (optional value)
Option values are the first argument after the string. If a value is required, it does not matter whether the value has leading white
space or not. See note.
Note
Optional values do not accept " " (space) as a separator.
Note
The format for the $options and $longopts is almost the same, the only difference is that $longopts takes an array of options
(where each element is the option) whereas $options takes a string (where each character is the option).
RETURN VALUES
This function will return an array of option / argument pairs or FALSE on failure.
Note
The parsing of options will end at the first non-option found, anything that follows is discarded.
CHANGELOG
+--------+---------------------------------------------------+
|Version | |
| | |
| | Description |
| | |
+--------+---------------------------------------------------+
| 5.3.0 | |
| | |
| | Added support for "=" as argument/value separa- |
| | tor. |
| | |
| 5.3.0 | |
| | |
| | Added support for optional values (specified |
| | with "::"). |
| | |
| 5.3.0 | |
| | |
| | Parameter $longopts is available on all systems. |
| | |
| 5.3.0 | |
| | |
| | This function is no longer system dependent, and |
| | now works on Windows, too. |
| | |
+--------+---------------------------------------------------+
EXAMPLES
Example #1
getopt(3) example: The basics
<?php
// Script example.php
$options = getopt("f:hp:");
var_dump($options);
?>
shell> php example.php -fvalue -h
The above example will output:
array(2) {
["f"]=>
string(5) "value"
["h"]=>
bool(false)
}
Example #2
getopt(3) example: Introducing long options
<?php
// Script example.php
$shortopts = "";
$shortopts .= "f:"; // Required value
$shortopts .= "v::"; // Optional value
$shortopts .= "abc"; // These options do not accept values
$longopts = array(
"required:", // Required value
"optional::", // Optional value
"option", // No value
"opt", // No value
);
$options = getopt($shortopts, $longopts);
var_dump($options);
?>
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option
The above example will output:
array(6) {
["f"]=>
string(11) "value for f"
["v"]=>
bool(false)
["a"]=>
bool(false)
["required"]=>
string(5) "value"
["optional"]=>
string(14) "optional value"
["option"]=>
bool(false)
}
Example #3
getopt(3) example: Passing multiple options as one
<?php
// Script example.php
$options = getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac
The above example will output:
array(2) {
["a"]=>
array(3) {
[0]=>
bool(false)
[1]=>
bool(false)
[2]=>
bool(false)
}
["c"]=>
bool(false)
}
SEE ALSO
$argv.
PHP Documentation Group GETOPT(3)