CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
recursive replace string in all files with specific name

 



mozkill
Novice

Oct 21, 2002, 3:48 PM

Post #1 of 5 (3831 views)
recursive replace string in all files with specific name Can't Post

i need a script that recursively searches a directory for files of a specific name like "*.props", and it replaces a string in each of the matching files that it finds.


PerlKid
stranger

Oct 22, 2002, 1:27 AM

Post #2 of 5 (3824 views)
Re: [mozkill] recursive replace string in all files with specific name [In reply to] Can't Post

try using File::Find for this.
--Philip
FuzzyLogic at PerlMad dot com


Caunter
New User

Nov 1, 2002, 6:11 AM

Post #3 of 5 (3796 views)
Re: [mozkill] recursive replace string in all files with specific name [In reply to] Can't Post

That's even simpler, friend :)

$ find . -name *.props -exec perl -p -i -e 's/string_to_replace/new_string/' {} \;

Best regards:)


microwebber
Novice

Nov 7, 2002, 8:22 AM

Post #4 of 5 (3783 views)
Re: [mozkill] recursive replace string in all files with specific name [In reply to] Can't Post

Did anyone write a script for you? If not I can write one.

Tricia,

yorkeylady@earthlink.net
Thanks
Tricia
yourkeylady@earthlink.net


Cyré
New User

May 2, 2008, 3:14 PM

Post #5 of 5 (3516 views)
Re: [Caunter] recursive replace string in all files with specific name [In reply to] Can't Post


In Reply To
$ find . -name *.props -exec perl -p -i -e 's/string_to_replace/new_string/' {} \;


So, let's say I want all *.php, *.htm*, *.xml, *.css, *.js files to be stripped of preceding spaces (indent code) and double newlines/linebreaks, for example,
this:

Code
<?php 

// ---------------------------------------------------------------------------
//
// LICENSE:
//
// This file is part of . and all its parts are licensed under
// the GPL version 2. see: http://www..net/help/help_about_gpl.php
// for more information.
//
// ---------------------------------------------------------------------------

// First line defense.
if (file_exists(dirname(__FILE__)."/first_defense.php")) {
include_once(dirname(__FILE__)."/first_defense.php");
block_refererspam();

}


define('__SILENT__', TRUE);

// apparently also defined in pv_core
// $marco (who hates php notices)
// define('IN', TRUE);

define('LIVEPAGE', TRUE);

include_once("v_core.php");

should become:

Code
<?php 
if (file_exists(dirname(__FILE__)."/first_defense.php")) {
include_once(dirname(__FILE__)."/first_defense.php");
block_refererspam();
}
define('__SILENT__', TRUE);
define('LIVEPAGE', TRUE);
include_once("v_core.php");


Does anyone have that laying around? ;-)

Stripping tabs, spaces, empty lines and comments from an entire webserver doc root (plus subdirs), without leaving a backup (assume a full backup exists).

Also, in HTML of course double spaces are only important if they are enclosed by <PRE> tags, right?
One can make a long line of javascript, css and html code (except the text echoes and such). I used to have a handy DOS tool for this, but it got lost over the years.

I can run this as root from a centos linux shell, so no crap with permissions. (And yes, I know, being root is bad. Not for this task though..)

The script can be run by me as somescript.sh, like

Code
#!/bin/sh 
$ perl -pi -e 's|[old_string]|[new_string]|g' [file]


Thanks in advance! I'll credit you on my website if you can make this for me. ;-) I love Perl and RegExp. They're my favorite tools!


(This post was edited by Cyré on May 2, 2008, 3:22 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives