Noob wants Q&D pointer, regexp replacement

(Gunnar Hjalmarsson) #1

William James wrote:

···

ssi@interlog.com 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

(Charles Mills) #2

Gunnar Hjalmarsson wrote:

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

I assume this isn't a troll.
Can it be done in one line (without semicolons)?

#!/usr/bin/env ruby
contents = File.read(ARGV[0]).gsub(/regexp/, File.read(ARGV[1]))
File.open(ARGV[0], 'w') { |f| f.write(contents) }

-Charlie

···

> ssi@interlog.com wrote:

(Gunnar Hjalmarsson) #3

Charles Mills wrote:

Gunnar Hjalmarsson wrote:

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

I assume this isn't a troll.

Thanks, but I'm afraid you missed the point. _William_ is trolling at comp.lang.perl.misc, and I was hoping you were able to keep him busy here...

···

ssi@interlog.com wrote:

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