Search-replace in multiple files?

  • Thread starter Roger Johansson
  • Start date
R

Roger Johansson

I have been trying to learn more about regular expressions. I have
tried many programs and read a lot of help files.

I finally managed to use regexp in BK-Replace-em to change all the
<body background...> tags in a number of html files into a plain
<body>, for example. My first success with regexpressions!

Search expression, replace expression:
<[Bb][Oo][Dd][Yy][^>]*> <body>

I learned how to use the Tcl/Tk script visual_regexp.tcl to test
regular expressions.

Now I want to learn how to use perl to perform the same type of tasks,
replacing one expression with another in multiple files, for example
in all files in a certain directory.

I have installed ActivePerl 5.8 and have read pod files for many
hours. But I still cannot write a program for this kind of task in
perl. I am a total beginner in using perl.

Could somebody please show me how to write a perl script to change a
number of files with a regexp search and replace, like I did in
BK-Replacem?

I have read perlrequick and perlretut and I understand how to use
regular expressions, but how do I handle the directory and files, and
maybe I have to create loops to read in each file into a string,
etc..?

A practical example script would help a lot.
 
G

Gunnar Hjalmarsson

Roger said:
Now I want to learn how to use perl to perform the same type of
tasks, replacing one expression with another in multiple files, for
example in all files in a certain directory.

I have installed ActivePerl 5.8 and have read pod files for many
hours. But I still cannot write a program for this kind of task in
perl. I am a total beginner in using perl.
http://learn.perl.org/

but how do I handle the directory and files, and maybe I have to
create loops to read in each file into a string, etc..?

Something like that.

http://learn.perl.org/
 
K

ko

Roger Johansson wrote:

[snip]
Now I want to learn how to use perl to perform the same type of tasks,
replacing one expression with another in multiple files, for example
in all files in a certain directory.

If its a simple search/replace, you can do it with a one-liner from your
shell:

*nix:
perl -p -i.orig -e 's/SEARCH/REPLACE/g' *.html

WINDOWS (wrapped, but should be on one line):
perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
s/SEARCH/REPLACE/g" *.html

These commands will do replacement on HTML files in the current
directory. In the second example, you need the BEGIN block because the
Windows shell doesn't do wildcard expansion. 'perlrun' will give you a
better idea of what you can do with the -i, -p, and -e command line
switches.

[snip]
I have read perlrequick and perlretut and I understand how to use
regular expressions, but how do I handle the directory and files, and
maybe I have to create loops to read in each file into a string,
etc..?

'perlsyn' will help you understand loops, among other things. The
perlfaqs are all good if you haven't read them already - 'perlfaq5' will
help with reading files, etc. No offense, but it usually takes a while
(messing around with them) to *really* understand regular expressions.
I've read 'perlrequick' and 'perlretut' a few times, and still have a
way to go...

HTH - keith
 
R

Roger Johansson

ko said:
If its a simple search/replace, you can do it with a one-liner from your
shell:
WINDOWS (wrapped, but should be on one line):
perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
s/SEARCH/REPLACE/g" *.html

These commands will do replacement on HTML files in the current
directory. In the second example, you need the BEGIN block because the
Windows shell doesn't do wildcard expansion. 'perlrun' will give you a
better idea of what you can do with the -i, -p, and -e command line
switches.

Thanks. It works.

I had some practical problems because a DOS Prompt window does not
allow copy and paste from the windows clipboard, so I would have to
write everything manually in the DOS window.
I had to change autoexec.bat to get my swedish keyboard to work in DOS
windows.

I finally found a way to do it in my file handler Total Commander, and
that worked, but it would be nice to have a DOS console program which
was more useful than the DOS Prompt window. How do you perl
programmers solve that problem? Is there a DOS console program you can
use to work in which allows copying and pasting from the windows
environment?
A window which doesn't close every time a script has ended, and with
better background color, better font, etc..

I tried to put your line of code into a .pl script, like this:

#! ./perl
#
# search and replace in all html files in current directory

use strict;
use warnings;

{
perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
s/SEARCH/REPLACE/g" *.html
}

{
print "Press [ ENTER ] to continue\n";
<STDIN>;
}

But it didn't work. I probably made some stupid mistake.
(I unwrapped the long line, so it is not that problem)

I found several online books on learn.perl.org but I haven't had time
to read them yet. I will tonight :)
 
T

Tad McClellan

I tried to put your line of code into a .pl script, like this:

#! ./perl
#
# search and replace in all html files in current directory

use strict;
use warnings;

{
perl -p -i.orig -e "BEGIN {@ARGV = map { glob } @ARGV }
s/SEARCH/REPLACE/g" *.html
}

{
print "Press [ ENTER ] to continue\n";
<STDIN>;
}

But it didn't work.


The switches go on the shebang (#!) line.

#!perl -p -i.orig
BEGIN {@ARGV = map { glob } @ARGV }
s/SEARCH/REPLACE/g;

or, you read perlrun.pod to see what the switches do for you,
then write code that does it yourself:

#!perl
BEGIN {@ARGV = map { glob } @ARGV }
$^I = '.orig'; # turn on in-place editing
while ( <> ) {
s/SEARCH/REPLACE/g;
print;
}
 
B

Bob Walton

Roger said:
....
I had some practical problems because a DOS Prompt window does not
allow copy and paste from the windows clipboard, so I would have to


<offtopic>
Sure it does. Right-click the DOS window's title bar and select edit,
then paste. To go the other way easily, modify the properties of the
DOS window, checking the box "quick edit" on the "misc" tab. Then
highlight a rectangular region with your mouse and push the enter key.
Some of that might be dependent upon what version of Windoze you have --
that stuff works for Windoze 98SE and NT 4 sp 6A at least; I don't know
about the rest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top