Noob wants Q&D pointer, regexp replacement.

Discussion in 'Perl Misc' started by ssi@interlog.com, Aug 11, 2005.

  1. Guest

    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
    , Aug 11, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    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
    Paul Lalli, Aug 11, 2005
    #2
    1. Advertising

  3. rcurzon Guest

    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
    rcurzon, Aug 11, 2005
    #3
  4. 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
    William James, Aug 11, 2005
    #4
  5. William James wrote:
    > 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
    Gunnar Hjalmarsson, Aug 11, 2005
    #5
  6. rcurzon Guest

    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. ;-)
    rcurzon, Aug 12, 2005
    #6
  7. 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
    John W. Krahn, Aug 12, 2005
    #7
  8. Dave Weaver Guest

    On 11 Aug 2005 13:51:33 -0700, rcurzon <> 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"
    Dave Weaver, Aug 12, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    663
    Chris Torek
    Feb 4, 2005
  2. daniel åkerud
    Replies:
    7
    Views:
    136
    Jari Williamsson
    Jan 25, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    328
    7stud --
    Aug 21, 2009
  4. Rick
    Replies:
    1
    Views:
    86
    Gunnar Hjalmarsson
    Oct 31, 2006
  5. Replies:
    2
    Views:
    100
Loading...

Share This Page