Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl inline replace with execution results

by skerr1 (Sexton)
on Oct 23, 2005 at 05:29 UTC ( [id://502292]=perlquestion: print w/replies, xml ) Need Help??

skerr1 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Folks,
On a SunOS 5.6 machine running Perl 5.6.1
From the command line I want to execute a perl inline replace where I want to replace the found text with the execution results of the found text. So I want to search for "cmd" and replace it with the results of executing "cmd". Here is the code I've been playing with unsuccessfully:

perl -i -p -e "s/^(mytool\.pl .*)/`\$1`/" test.template

Help is appreciated.

Thanks!

Shannon Kerr

Replies are listed 'Best First'.
Re: Perl inline replace with execution results
by Zaxo (Archbishop) on Oct 23, 2005 at 06:04 UTC

    sk found two problems, here's a couple more. Your mytool.pl line will not run without a path indication unless its directory is in your $PATH. Double quoting is undesirable since it will make the shell try to interpolate. This works for me,

    $ cat >mytool.pl #!/usr/bin/perl print 'Substitute string'; $ chmod 755 mytool.pl $ cat >test.tmpl This is mytool.pl foo $ perl -pe's/(mytool\.pl\b.*)/`.\/$1`/e' test.tmpl This is Substitute string $
    It was necessary to escape '/' in the path because we used it for the substitution delimiter.

    After Compline,
    Zaxo

      (Updated to thank the proper folks. :) Thanks for all the friendly reminders)

      Excellent zaxo and sk! You make it look too easy. :)

      zaxo, you are right about the double quotes and that is why I had the "\" before the "$1". In my original try I had single quotes, but there I saw that it wasn't executing, but rather just replacing with exactly what I wanted to execute. So, I want to run this from the cmd line and I'll mix up what the two of you both shared.

      I finally went with this (mytool.pl is in my PATH):

      perl -i.bak -pe 's/^(mytool\.pl .*)/`$1`/e' test.template

      Nice. Thank you both!

      Shannon Kerr
Re: Perl inline replace with execution results
by sk (Curate) on Oct 23, 2005 at 05:39 UTC
    Remove the \ before $1 and you need /e

    If you run it without /e you will notice the string becomes `printhi`. Now you want to eval this expression and the /e takes care of that.

    This works for me

    #!/usr/bin/perl use strict; use warnings; my $str = "printmeprinthi"; $str =~ s/printme(.*)$/`$1`/e; print $str,$/;

    my printhi program is

    #!/bin/sh echo hi

    output

    hi

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://502292]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-30 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found