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

Re: search and replace text in multiple files in a directory

by willyyam (Priest)
on Apr 02, 2005 at 19:46 UTC ( [id://444413]=note: print w/replies, xml ) Need Help??


in reply to search and replace text in multiple files in a directory

What I do is I have a script in my ~/bin directory called fixfile.pl that looks like this:

!/usr/bin/perl -p -i s/expression_to_replace/replacement_expression/gs;

My program is much longer, because each time I find a regex that works I leave it in the program, commented, and add my new regex. That way I create a library of useful regexes. To use the program I just go to the directory I want to make the changes in, and run (for your example) fixfile.pl *.html and the regex is performed on all files with the extension .html. The reason I find this useful is because I can leverage the power of a perl one-liner without losing the record of what I've been doing, and it lets me use my familiar shell globbing operators.

Replies are listed 'Best First'.
Re^2: search and replace text in multiple files in a directory
by Anonymous Monk on Apr 04, 2005 at 10:52 UTC
    I think the script you are trying to write can be wrote with Tie::File . Read the documentation about it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://444413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found