Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by willyyam (Priest)
on Apr 02, 2005 at 19:46 UTC ( #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
Node Status?
node history
Node Type: note [id://444413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2019-12-08 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (162 votes). Check out past polls.

    Notices?