Oneliner problem with < and > in variable

Discussion in 'Perl Misc' started by Ansim, Sep 14, 2007.

  1. Ansim

    Ansim Guest

    Hello,

    I have this oneliner
    $_PERL -ni -we 'BEGIN{@content='$_new_start'} if(/'$_start'/){s//
    @content/;print}else{print}' SMOinfo.xml

    where $_start is <Action name="abc" id="_XX_"> , a xml pragma, and
    $_new_start is <!-- <Action.....>, same xml pragma but with comment
    marks first.

    When this oneliner is executet i receives this error:
    "Unterminated <> operator at -e line 1.

    I've tried several settings in the @content='$_new_start' statement,
    everyone failing with different error codes.
    Tried to change the ' to ` or " or "' or ´ and without.


    The idea is to, in a script, auto magically comment out Action
    settings in the xml file, if they are commented in another file.

    The one liner is executet once more and end the comment with

    $_PERL -ni -we 'BEGIN{@content='$_new_end'} if(/'$_end'/){s//
    @content/;print}else{print}' SMOinfo.xml
    where $_end="</Action name="abc" id="_XX_">", $_new_end="</Action
    id="_XX_"> -->"

    Any suggestions on how to solve this?

    BR
    //Anders
    Ansim, Sep 14, 2007
    #1
    1. Advertising

  2. Ansim <> writes:

    > I have this oneliner
    > $_PERL -ni -we 'BEGIN{@content='$_new_start'} if(/'$_start'/){s//
    > @content/;print}else{print}' SMOinfo.xml
    >
    > where $_start is <Action name="abc" id="_XX_"> , a xml pragma, and
    > $_new_start is <!-- <Action.....>, same xml pragma but with comment
    > marks first.


    Could you try to cut'n'paste exactly what you're writing in you
    oneliner. In the above $_new_start isn't anything but undef.

    //Makholm
    Peter Makholm, Sep 14, 2007
    #2
    1. Advertising

  3. On Fri, 14 Sep 2007 01:41:19 -0700, Ansim <> wrote:

    >I have this oneliner
    >$_PERL -ni -we 'BEGIN{@content=3D'$_new_start'} if(/'$_start'/){s//
    >@content/;print}else{print}' SMOinfo.xml


    Quote the inner single quotes. BTW: a shell problem, not a Perl one...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Sep 14, 2007
    #3
  4. Ansim

    Ansim Guest

    If i set @content='"$_new_start"' and if(/<Activity.*_MM_FRP_FILE"\>/)

    _new_start="<!-- <Activity Name="Firmware Upgrade"
    Build_id="_MM_FRP_FILE">" by some grep, nawk.

    (Of course not the first and last ".)

    i get following output:

    Unterminated <> operator at -e line 1.

    and nothing is done in the SMOinfo.xml

    But, if i set @content='"$_start"' where _start="<Activity
    Name="Firmware Upgrade" Build_id="_MM_FRP_FILE">"
    then the line in SMOinfo.xml is changed to
    Activity Name="Firmware Upgrade" Build_id="_MM_FRP_FILE" , note! the
    < > is removed. Not what i wanted.

    The oneliner is executed inside a ksh script.

    //Anders
    Ansim, Sep 14, 2007
    #4
  5. On Fri, 14 Sep 2007 05:20:56 -0700, Ansim <> wrote:

    >If i set @content='"$_new_start"' and if(/<Activity.*_MM_FRP_FILE"\>/)
    >
    >_new_start="<!-- <Activity Name="Firmware Upgrade"
    >Build_id="_MM_FRP_FILE">" by some grep, nawk.
    >
    >(Of course not the first and last ".)
    >
    >i get following output:
    >
    >Unterminated <> operator at -e line 1.
    >
    >and nothing is done in the SMOinfo.xml
    >
    >But, if i set @content='"$_start"' where _start="<Activity
    >Name="Firmware Upgrade" Build_id="_MM_FRP_FILE">"
    >then the line in SMOinfo.xml is changed to
    >Activity Name="Firmware Upgrade" Build_id="_MM_FRP_FILE" , note! the
    >< > is removed. Not what i wanted.
    >
    >The oneliner is executed inside a ksh script.


    I think all shells behave more or less the same wrt basic quoting. Can
    you post the complete oneliners in their variations and explain how
    they fail to work please?

    From the first post I gathered that you had a quoting problem of
    single quotes inside single quotes. You overcome it like so:

    'something \'strange\' indeed'

    Here you brought up double quotes. I could not follow your words in
    detail admittedly *also* because I'm in a hurry. But remember that
    double quotes *interpolate* in Perl, and this may matter.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Sep 14, 2007
    #5
  6. Ansim wrote:
    >
    > I have this oneliner
    > $_PERL -ni -we 'BEGIN{@content='$_new_start'} if(/'$_start'/){s//
    > @content/;print}else{print}' SMOinfo.xml
    >
    > where $_start is <Action name="abc" id="_XX_"> , a xml pragma, and
    > $_new_start is <!-- <Action.....>, same xml pragma but with comment
    > marks first.
    >
    > When this oneliner is executet i receives this error:
    > "Unterminated <> operator at -e line 1.
    >
    > I've tried several settings in the @content='$_new_start' statement,
    > everyone failing with different error codes.
    > Tried to change the ' to ` or " or "' or ´ and without.
    >
    >
    > The idea is to, in a script, auto magically comment out Action
    > settings in the xml file, if they are commented in another file.
    >
    > The one liner is executet once more and end the comment with
    >
    > $_PERL -ni -we 'BEGIN{@content='$_new_end'} if(/'$_end'/){s//
    > @content/;print}else{print}' SMOinfo.xml
    > where $_end="</Action name="abc" id="_XX_">", $_new_end="</Action
    > id="_XX_"> -->"
    >
    > Any suggestions on how to solve this?


    In perl one-liners:

    -n ' if ( // ) { s///; print } else { print } '

    Is usually written as:

    -p ' s/// '

    So:

    $_PERL -ni -we 'BEGIN{@content='$_new_start'}
    if(/'$_start'/){s//@content/;print}else{print}' SMOinfo.xml

    *Should* be written as:

    $_PERL -pi -we 'BEGIN{$content='$_new_start'} s/'$_start'/$content/' SMOinfo.xml

    Or perhaps just:

    $_PERL -pi -we 's/'$_start'/'$_new_start'/' SMOinfo.xml


    If that doesn't work then you could try something like this:

    $_PERL -spi -we 's/$x/$y/o' -- -x="$_start" -y="$_new_start" SMOinfo.xml




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Sep 14, 2007
    #6
  7. Ansim

    Ansim Guest

    Thanks all for the help.

    Glenn and Michal helped a lot.

    The rest helped me with understanding the importance of qouting.
    Perl interpreted the < and > in a strange way othervise.

    BR
    //Anders
    Ansim, Sep 18, 2007
    #7
    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. Alex Martelli

    "number-in-base" ``oneliner''

    Alex Martelli, Oct 29, 2004, in forum: Python
    Replies:
    21
    Views:
    650
    Andrea Griffini
    Nov 1, 2004
  2. Replies:
    2
    Views:
    276
    Steven Bethard
    Nov 1, 2004
  3. anton muhin
    Replies:
    1
    Views:
    286
    anton muhin
    Nov 1, 2004
  4. Jon Baer
    Replies:
    3
    Views:
    121
    Pit Capitain
    Jan 27, 2006
  5. John Carter
    Replies:
    3
    Views:
    496
    Robert Klemme
    Mar 30, 2007
Loading...

Share This Page