Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Search-replace in multiple files?

Reply
Thread Tools

Search-replace in multiple files?

 
 
Roger Johansson
Guest
Posts: n/a
 
      10-09-2003
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.

--
Roger J.

(My email address is a spam trap, do not use it)
(Use my real email address, if you know it.)
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-09-2003
Roger Johansson wrote:
> 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/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      10-09-2003
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

 
Reply With Quote
 
Roger Johansson
Guest
Posts: n/a
 
      10-09-2003
ko <(E-Mail Removed)> wrote:

>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


--
Roger J.

(My email address is a spam trap, do not use it)
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-09-2003
Roger Johansson <(E-Mail Removed)> wrote:
> ko <(E-Mail Removed)> wrote:
>
>>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


> 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;
}


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      10-10-2003
Roger Johansson wrote:

> ko <(E-Mail Removed)> wrote:
>

....
> 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.

</offtopic>


> write everything manually in the DOS window.
> I had to change autoexec.bat to get my swedish keyboard to work in DOS
> windows.



--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple ISPs and Multiple IP Ranges from Each ISP Chennak Cisco 10 06-08-2005 09:29 PM
IP Address Scheme for Multiple DMZs on Multiple PIXs Scotchy Cisco 2 10-07-2004 03:35 PM
Preventing multiple form submissions (multiple postbacks) Diane Selby ASP .Net 4 06-25-2004 09:50 PM
multiple dropdown lists = multiple data readers??? Andrew Kidd ASP .Net 3 04-22-2004 03:23 PM
Re: One solution containing multiple projects and multiple hosts Stephen Miller ASP .Net 0 06-24-2003 12:55 PM



Advertisments