Sponsored Content
Top Forums Shell Programming and Scripting Script to find & replace a multiple lines string across multiple php files and subdirectories Post 302604153 by agama on Saturday 3rd of March 2012 11:07:47 AM
Old 03-03-2012
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:
Code:
#!/usr/bin/env ksh

cd /directory/path/where/you/want/to/start
find . -name "*.php" | while read file
do
    echo "munging: $file"             # nice to see progress as it works
    mv "$file" "$file-"      # back it up

    ### insert sed or awk here ####  the sed in next line is for illustration only 
    sed 's/nosuchstringinthefile/noscuhreplacement/' "$file-" >"$file"
    if (( $? > 0 ))            # handle failure by putting the file back in place
    then
        echo "edit of $file failed" >&2
        mv "$file-" "$file"             # restore original
    else
        rm "$file-"               # worked, delete backup 
    fi
done



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):

Code:
chmod 755 delete_frm_php.ksh

You then can invoke your script from the command line by typing delete_frm_php.ksh
 

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

str_replace - Replace all occurrences of the search string with the replacement string

SYNOPSIS
mixed str_replace (mixed $search, mixed $replace, mixed $subject, [int &$count]) DESCRIPTION
This function returns a string or an array with all occurrences of $search in $subject replaced with the given $replace value. If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace(3). PARAMETERS
If $search and $replace are arrays, then str_replace(3) takes a value from each array and uses them to search and replace on $subject. If $replace has fewer values than $search, then an empty string is used for the rest of replacement values. If $search is an array and $replace is a string, then this replacement string is used for every value of $search. The converse would not make sense, though. If $search or $replace are arrays, their elements are processed first to last. o $search - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles. o $replace - The replacement value that replaces found $search values. An array may be used to designate multiple replacements. o $subject - The string or array being searched and replaced on, otherwise known as the haystack. If $subject is an array, then the search and replace is performed with every entry of $subject, and the return value is an array as well. o $count - If passed, this will be set to the number of replacements performed. RETURN VALUES
This function returns a string or an array with the replaced values. EXAMPLES
Example #1 Basic str_replace(3) examples <?php // Provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // Provides: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?> Example #2 Examples of potential str_replace(3) gotchas <?php // Order of replacement $str = "Line 1 Line 2 Line 3 Line 4 "; $order = array(" ", " ", " "); $replace = '<br />'; // Processes 's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); // Outputs F because A is replaced with B, then B is replaced with C, and so on... // Finally E is replaced with F, because of left to right replacements. $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // Outputs: apearpearle pear // For the same reason mentioned above $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); echo $output; ?> NOTES
Note This function is binary-safe. Caution Replacement order gotcha Because str_replace(3) replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document. Note This function is case-sensitive. Use str_ireplace(3) for case-insensitive replace. SEE ALSO
str_ireplace(3), substr_replace(3), preg_replace(3), strtr(3). PHP Documentation Group STR_REPLACE(3)
All times are GMT -4. The time now is 12:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy