Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Noob wants Q&D pointer, regexp replacement.

Reply
Thread Tools

Noob wants Q&D pointer, regexp replacement.

 
 
ssi@interlog.com
Guest
Posts: n/a
 
      08-11-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
rcurzon
Guest
Posts: n/a
 
      08-11-2005
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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-11-2005
(E-Mail Removed) 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...
> --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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2005
William James wrote:
> (E-Mail Removed) 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 ...

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
rcurzon
Guest
Posts: n/a
 
      08-12-2005
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.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-12-2005
(E-Mail Removed) 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.


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


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      08-12-2005
On 11 Aug 2005 13:51:33 -0700, rcurzon <(E-Mail Removed)> wrote:
> 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"

 
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
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments