Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As I find this find statement very handy but because I often
forget commands I use seldom, I have put this useful thing
into a useable script.
Maybe a silly idea to execute a shell script invoking perl
from a perlscript, but the target "Get it working" is reached :-)
#!/usr/bin/perl -w # # replace_in_tree replaces stringx by stringy in files from current di +rectory # recursiveley and backups the original files to *.bak like # find . -type f | xargs -n 255 perl -pi.bak -e 's/string x/string y/g +' # does. # # Author : Sascha Wuestemann # License: Artistic GPL # eMail : Sascha.Wuestemann@epost.de use strict; sub usage { print <<EOF; usage: replace_in_tree <to_be_replaced> <replacement> <filematcher> replaces the string "to_be_replaced" with "replacement" from the current directory downwards the filetree in files matching filematcher, e.g. "*" or "*.html" example: replace_in_tree "Author: Bill Gates" "Author: Larry Wall" *.html EOF } sub exec_replace { open(GUN,"find . -name \"$ARGV[2]\" -type f | xargs -n 255 perl -pi.ba +k -e 's/$ARGV[0]/$ARGV[1]/g'|")|| die "can't execute shell commands:$ +!\n"; close(GUN)|| warn "couldn't close shell:$!\n"; } if (not $ARGV[2]) { usage; exit; } else { exec_replace } exit;



--
there are no silly questions
killerhippy

In reply to Re: RE: Replace across files (extreme newbie) by Anonymous Monk
in thread Replace across files (extreme newbie) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found