Noob wants Q&D pointer, regexp replacement.

S

ssi

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

Paul Lalli

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
 
R

rcurzon

Thanks much Paul...

"Give a man a fish; you have fed him for today. Teach a man to fish;
and you have fed him for a lifetime".

But if he only wants a fish in a lifetime, why not just throw him a cod
;-).

thanks again... for saving me from learning to fish ... again...
-R
 
W

William James

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

Richard, since you use Perl so infrequently that you have
to start on page 1, why not switch to an easier language?
Once you begin using Ruby, you will welcome opportunities
to use it.

And note the arrogance and rudeness of "Perl gurus".
They will condescend to help you only when they are
"bored".


#!ruby

# This program 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 "target" being replaced with the contents of the first
# file.

contents = IO.read( ARGV.shift ).chomp

# Start in-place processing; make backups with the extension.
$-i = ".bak"

while line = gets
print line.sub( /target/, contents )
end
 
G

Gunnar Hjalmarsson

William said:
Richard, since you use Perl so infrequently that you have
to start on page 1, why not switch to an easier language?
Once you begin using Ruby, you will welcome opportunities
to use it.

And note the arrogance and rudeness of "Perl gurus".
They will condescend to help you only when they are
"bored".

#!ruby

# This program 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 "target" being replaced with the contents of the first
# file.

contents = IO.read( ARGV.shift ).chomp

# Start in-place processing; make backups with the extension.
$-i = ".bak"

while line = gets
print line.sub( /target/, contents )
end

Anybody over at comp.lang.ruby who has a really trivial task for this
William character who obviously has nothing to do? Be sure it would be
appreciated at comp.lang.perl.misc.
 
R

rcurzon

Ironically I even use awk regularly enough to do this easily, but ... I
just signed up with an ISP who only supports perl, I was annoyed.

Just when it's all that "no-character-unused" punctation is gone out of
my head, gotta learn it again... or maybe not, thanks to Paul's
boredom. ;-)
 
J

John W. Krahn

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.

perl -i.bak -pe'BEGIN{$regex=qr/@{[shift]}/;$file2=qx/cat
"@{[pop]}"/}s/$regex/$file2/og' 'regex' file1 file2


John
 
D

Dave Weaver

Thanks much Paul...

"Give a man a fish; you have fed him for today. Teach a man to fish;
and you have fed him for a lifetime".

Or maybe more appropriately in this case:

"Give a man a program, he'll be frustrated for a day. Teach a man to
program, he'll be frustrated for a lifetime"
 

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

Forum statistics

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

Latest Threads

Top