Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Noob wants Q&D pointer, regexp replacement.

3 views
Skip to first unread message

Gunnar Hjalmarsson

unread,
Aug 11, 2005, 5:26:05 PM8/11/05
to
William James wrote:
> s...@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

unread,
Aug 12, 2005, 1:16:49 AM8/12/05
to

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

Gunnar Hjalmarsson

unread,
Aug 12, 2005, 1:49:11 AM8/12/05
to

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

0 new messages