Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: perl one liner to replace matching string

by Athanasius (Archbishop)
on Jun 10, 2018 at 07:48 UTC ( [id://1216295]=note: print w/replies, xml ) Need Help??


in reply to perl one liner to replace matching string

Hello vinoth.ree,

With warnings:

use strict; use warnings; while (<DATA>) { s/(@Filetyp\s*?:)/\1c/; print; } __DATA__ @header_start @Titel : init @Filename : test.c @Filetyp : @Version : 0001 @Produkt : xx @delivery : xx @date : 20160610 @header_end

Output:

0:54 >perl 1895_SoPW.pl Possible unintended interpolation of @Filetyp in string at 1895_SoPW.p +l line 18. \1 better written as $1 at 1895_SoPW.pl line 18. Global symbol "@Filetyp" requires explicit package name (did you forge +t to declare "my @Filetyp"?) at 1895_SoPW.pl line 18. Execution of 1895_SoPW.pl aborted due to compilation errors. 17:41 >

Perl sees @Filetyp as an (empty) array. Escape the sigil (and change \1 to $1):

s/(\@Filetyp\s*?:)/$1 c/;.

Output:

17:44 >perl 1895_SoPW.pl @header_start @Titel : init @Filename : test.c @Filetyp : c @Version : 0001 @Produkt : xx @delivery : xx @date : 20160610 @header_end 17:44 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: perl one liner to replace matching string
by vinoth.ree (Monsignor) on Jun 10, 2018 at 08:05 UTC

    I tried the below perl one liner to replace the "@Filetyp :" as "@Filetyp :c"

    my $ext='c'; qx("perl -p -i -e \"s/(\@Filetyp\\s*?:)/\\1$ext/\" /path/test.c");

    I am already doing it.Could you pls try like above perl one liner to replace the string?


    All is well. I learn by answering your questions...

      Ok, I get the same result as you reported. Playing around with it, I had to add two additional backslashes to get it to work as desired (not sure why):

      my $ext='c'; qx("perl -p -i.bak -e \"s/(\\\@Filetyp\\s*?:)/\\1$ext/\" ./path/test.c +"); # ^^

      (Note: I also had to specify an extension for the backup file.) This is on Windows; if your O/S is different, it may treat the backslashes differently.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        In response to: not sure why

        Similar findings: here and it was pretty heavily analyzed by AnomalousMonk in the following posts

        You are right I am writing this for windows, If it is linux/unix I could have used sed/awk.


        All is well. I learn by answering your questions...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found