Re: Noob wants Q&D pointer, regexp replacement.
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 11 Aug 2005 13:24:36 -0700
ssi@xxxxxxxxxxxx wrote:
> Hello, I do perl once every 5 years. Or longer. I hate having to read
> chapter 1, page 1 again every time ;-)...
>
> Can someone point me to a script that does almost what I want... and
> I'm sure I can touch it up to do what I want.
>
> I want to search file1 for regexp1. Each time I find it, I want to
> replace it with the entire contents of file2.
>
> When done, I want to replace file1 with the result.
>
> There are probably a hundred perl scripts on the web that do
> approximately that without me reading chapter 1 again ;-)...
>
> thanks for any pointer...
I got bored, so wrote this. Note that this kind of "help" should not
be expected in this newsgroup. You are much more likely to receive
helpful pointers to the documentation. In this case, that would be:
perldoc -f open
perldoc -f readline
perldoc -f print
perldoc perlretut
perldoc perlop (search for $/)
perldoc perlvar (search for $^I)
Regardless...
#!/usr/bin/perl
use strict;
use warnings;
my $contents;
{
my $file1 = shift;
local $/;
open my $fh, '<', $file1 or die "Cannot open $file1: $!\n";
$contents = <$fh>;
}
{
local $^I = '';
while (<>){
s/pattern/$contents/g;
print;
}
}
__END__
This script takes at least two file names on the command line. The
first file is read and its contents stored in $contents. All
subsequent files are edited "in place", the word "pattern" being
replaced with the contents of the first file.
Paul Lalli
.
- Follow-Ups:
- Re: Noob wants Q&D pointer, regexp replacement.
- From: rcurzon
- Re: Noob wants Q&D pointer, regexp replacement.
- References:
- Prev by Date: Re: perl program with "&"
- Next by Date: Re: lc() with undefined arg
- Previous by thread: Noob wants Q&D pointer, regexp replacement.
- Next by thread: Re: Noob wants Q&D pointer, regexp replacement.
- Index(es):
Relevant Pages
|