Loading ...
Sorry, an error occurred while loading the content.

Newline and Variables from Command-Line

Expand Messages
  • steve@rubysolutions.com
    I have a script for replacing various types of stuff recursively in files. it simply uses s/$pattern/$replacement/gi as the workhorse. where $pattern and
    Message 1 of 4 , Jan 4, 2001
    • 0 Attachment
      I have a script for replacing various types of stuff recursively in
      files. it simply uses s/$pattern/$replacement/gi as the workhorse.
      where $pattern and $replacement come from

      $replacement = pop(@ARGV);
      $pattern = pop(@ARGV);

      such that the script is called like
      replace [options] pattern replacement


      Problem:
      pattern can include all regex expressions and excape sequences such as
      "[\n\r]+" "" will get rid of all newlines

      but replacement takes everything litteraly, how do I make it so I can
      replace spaces with newlines like

      " " "\n"

      right now it replaces them with litteral \n,

      I've tried s/$pattern/eval {$replacement}/ge
      with no luck no matter what I do I can't get the \n to be reevaluated
      into a \n

      I would also like to be able to put variables into the replacment
      string like

      "(-[0-9]+)" "($1)"

      suggestions?
    • Sean Quinlan
      ... Unfortunately, I don t know an elegant solution for this. On unix. tab s and such can be entered literally when inside , but newlines are problematic. A
      Message 2 of 4 , Jan 4, 2001
      • 0 Attachment
        At 07:16 PM 1/4/01 -0000, you wrote:
        >I have a script for replacing various types of stuff recursively in
        >files. it simply uses s/$pattern/$replacement/gi as the workhorse.
        >where $pattern and $replacement come from
        >
        >$replacement = pop(@ARGV);
        >$pattern = pop(@ARGV);
        >
        >such that the script is called like
        >replace [options] pattern replacement
        >
        >
        >Problem:
        >pattern can include all regex expressions and excape sequences such as
        >"[\n\r]+" "" will get rid of all newlines
        >
        >but replacement takes everything litteraly, how do I make it so I can
        >replace spaces with newlines like
        >
        >" " "\n"
        >
        >right now it replaces them with litteral \n,
        Unfortunately, I don't know an elegant solution for this. On unix. tab's
        and such can be entered literally when inside " ", but newlines are
        problematic. A quick kludge would be to a substitution on the command line
        argument like:
        $replacement =~ s/\\n/\n/g;
        Which at least works within it's limited scope.

        >
        >I've tried s/$pattern/eval {$replacement}/ge
        >with no luck no matter what I do I can't get the \n to be reevaluated
        >into a \n
        >
        >I would also like to be able to put variables into the replacment
        >string like
        >
        >"(-[0-9]+)" "($1)"

        This can be handled with ee, such that your expresion, given the above
        provided on the command line, would be:
        s/$pattern/$replacement/eeg;
        Would, for your example, turn -2305 into -2305. However, this may cause
        other problems for you. For instance it would break the newline problem
        above. And you may have noticed, dropped the parens from the output.
        However, this can be gotten around if your replacement is entered as a
        string to be interpolated, such as entering (only tested under tcsh on a
        sun) '"\n"', which would work with ee; allowing something like
        '"(".$1")\n"' to work printing (from above)
        (-2305)
        (with newline).

        All in all, you may need to do a moderate ammount of decision making on how
        to go about doing the substitution by looking at the user input at runtime.
        Some of the new features added to regex's in 5.6 may also give you a better
        option, so I would recomend looking over the latest documentation. I hope
        this helps at least some.

        I hope this helps some at least.


        Sean P. Quinlan
        mailto:sean@...
        508-533-0582
      • Sean Quinlan
        Oops, sent with a critical typo. Please note ( .$1 ) n below should be ( .$1. ) n ... Sean P. Quinlan mailto:sean@quinlan.org 508-533-0582
        Message 3 of 4 , Jan 4, 2001
        • 0 Attachment
          Oops, sent with a critical typo. Please note '"(".$1")\n"' below should be
          '"(".$1.")\n"'

          >Would, for your example, turn -2305 into -2305. However, this may cause
          >other problems for you. For instance it would break the newline problem
          >above. And you may have noticed, dropped the parens from the output.
          >However, this can be gotten around if your replacement is entered as a
          >string to be interpolated, such as entering (only tested under tcsh on a
          >sun) '"\n"', which would work with ee; allowing something like
          >'"(".$1")\n"' to work printing (from above)
          >(-2305)
          >(with newline).
          >


          Sean P. Quinlan
          mailto:sean@...
          508-533-0582
        • steve@rubysolutions.com
          Thanks, that script needs to run under linux and NT your quotes worked find under bash and tsch on linux on NT I had only to reverse the quotes and do
          Message 4 of 4 , Jan 4, 2001
          • 0 Attachment
            Thanks, that script needs to run under linux and NT your quotes worked
            find under bash and tsch on linux on NT I had only to reverse the
            quotes and do
            "'('.$1.')'"

            I whish there was an easier way... I was hoping to not have to parse
            the command line for \n because then I would have to do the same for
            other escape sequesnces.

            How come I can put \n in the pattern and it works but not the
            replacement?


            --- In perl-beginner@egroups.com, Sean Quinlan <seanq@d...> wrote:
            > Oops, sent with a critical typo. Please note '"(".$1")\n"' below
            should be
            > '"(".$1.")\n"'
            >
            > >Would, for your example, turn -2305 into -2305. However, this may
            cause
            > >other problems for you. For instance it would break the newline
            problem
            > >above. And you may have noticed, dropped the parens from the
            output.
            > >However, this can be gotten around if your replacement is entered
            as a
            > >string to be interpolated, such as entering (only tested under tcsh
            on a
            > >sun) '"\n"', which would work with ee; allowing something like
            > >'"(".$1")\n"' to work printing (from above)
            > >(-2305)
            > >(with newline).
            > >
            >
            >
            > Sean P. Quinlan
            > mailto:sean@q...
            > 508-533-0582
          Your message has been successfully submitted and would be delivered to recipients shortly.