How can I use a shell variable to copy an sed pattern?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have SED patterns:



[^a-zA-Z0-9]

/b./s/.*c.. ([^ ]*) .*/1/p


and so on.



I need to pass these to an echo command as variables.



At the moment, I define the $pattern variable like so:



$pattern="[^a-zA-Z0-9]"


and then pipe it to echo, like so:



echo "$OUTPUT" | sed "s/$pattern/g"


But the code is not passing the pattern, but a command and returns the error



=[^a-zA-Z0-9]: command not found


What's going wrong?







share|improve this question






















  • Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
    – Zip
    Dec 7 '17 at 16:55











  • see also: unix.stackexchange.com/q/32907/117549
    – Jeff Schaller
    Dec 7 '17 at 17:00










  • [^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
    – Stéphane Chazelas
    Dec 7 '17 at 17:18







  • 1




    Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
    – PesaThe
    Dec 7 '17 at 17:57















up vote
0
down vote

favorite












I have SED patterns:



[^a-zA-Z0-9]

/b./s/.*c.. ([^ ]*) .*/1/p


and so on.



I need to pass these to an echo command as variables.



At the moment, I define the $pattern variable like so:



$pattern="[^a-zA-Z0-9]"


and then pipe it to echo, like so:



echo "$OUTPUT" | sed "s/$pattern/g"


But the code is not passing the pattern, but a command and returns the error



=[^a-zA-Z0-9]: command not found


What's going wrong?







share|improve this question






















  • Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
    – Zip
    Dec 7 '17 at 16:55











  • see also: unix.stackexchange.com/q/32907/117549
    – Jeff Schaller
    Dec 7 '17 at 17:00










  • [^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
    – Stéphane Chazelas
    Dec 7 '17 at 17:18







  • 1




    Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
    – PesaThe
    Dec 7 '17 at 17:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have SED patterns:



[^a-zA-Z0-9]

/b./s/.*c.. ([^ ]*) .*/1/p


and so on.



I need to pass these to an echo command as variables.



At the moment, I define the $pattern variable like so:



$pattern="[^a-zA-Z0-9]"


and then pipe it to echo, like so:



echo "$OUTPUT" | sed "s/$pattern/g"


But the code is not passing the pattern, but a command and returns the error



=[^a-zA-Z0-9]: command not found


What's going wrong?







share|improve this question














I have SED patterns:



[^a-zA-Z0-9]

/b./s/.*c.. ([^ ]*) .*/1/p


and so on.



I need to pass these to an echo command as variables.



At the moment, I define the $pattern variable like so:



$pattern="[^a-zA-Z0-9]"


and then pipe it to echo, like so:



echo "$OUTPUT" | sed "s/$pattern/g"


But the code is not passing the pattern, but a command and returns the error



=[^a-zA-Z0-9]: command not found


What's going wrong?









share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '17 at 17:27









ilkkachu

50.1k676138




50.1k676138










asked Dec 7 '17 at 16:45









Michael Riordan

385




385











  • Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
    – Zip
    Dec 7 '17 at 16:55











  • see also: unix.stackexchange.com/q/32907/117549
    – Jeff Schaller
    Dec 7 '17 at 17:00










  • [^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
    – Stéphane Chazelas
    Dec 7 '17 at 17:18







  • 1




    Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
    – PesaThe
    Dec 7 '17 at 17:57

















  • Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
    – Zip
    Dec 7 '17 at 16:55











  • see also: unix.stackexchange.com/q/32907/117549
    – Jeff Schaller
    Dec 7 '17 at 17:00










  • [^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
    – Stéphane Chazelas
    Dec 7 '17 at 17:18







  • 1




    Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
    – PesaThe
    Dec 7 '17 at 17:57
















Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
– Zip
Dec 7 '17 at 16:55





Does pattern='[^a-zA-Z0-9]' work better? You're not assigning a variable with $pattern=. Take a look at eval as an alternative too... I can make it into a proper answer if it works...
– Zip
Dec 7 '17 at 16:55













see also: unix.stackexchange.com/q/32907/117549
– Jeff Schaller
Dec 7 '17 at 17:00




see also: unix.stackexchange.com/q/32907/117549
– Jeff Schaller
Dec 7 '17 at 17:00












[^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
– Stéphane Chazelas
Dec 7 '17 at 17:18





[^a-zA-Z0-9] is a pattern but /b./s/.c.. ([^ ]) .*/1/p is a full sed script.
– Stéphane Chazelas
Dec 7 '17 at 17:18





1




1




Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
– PesaThe
Dec 7 '17 at 17:57





Just a little insight on the error. With dollar sign bash expands it as if it were a variable and because there was nothing in $pattern at that point, bash saw this: =[^a-zA-Z0-9]. And what's the next step? Executing the command with that name, thus the error.
– PesaThe
Dec 7 '17 at 17:57











2 Answers
2






active

oldest

votes

















up vote
2
down vote













$ pattern='[^a-zA-Z0-9]'
$ echo "123 ABC" | sed "s/$pattern/g"
sed: -e expression #1, char 16: unterminated `s' command
$ echo "123 ABC" | sed "s/$pattern//g"
123ABC
$ echo "123 ABC" | sed "s/$pattern/XYZ/g"
123XYZABC


And...




Shell variables are assigned without a leading $.







share|improve this answer



























    up vote
    1
    down vote













    Replace



    $pattern="[^a-zA-Z0-9]"


    by



    pattern="[^a-zA-Z0-9]"


    Shell variables are assigned without a leading $.






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409521%2fhow-can-i-use-a-shell-variable-to-copy-an-sed-pattern%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      $ pattern='[^a-zA-Z0-9]'
      $ echo "123 ABC" | sed "s/$pattern/g"
      sed: -e expression #1, char 16: unterminated `s' command
      $ echo "123 ABC" | sed "s/$pattern//g"
      123ABC
      $ echo "123 ABC" | sed "s/$pattern/XYZ/g"
      123XYZABC


      And...




      Shell variables are assigned without a leading $.







      share|improve this answer
























        up vote
        2
        down vote













        $ pattern='[^a-zA-Z0-9]'
        $ echo "123 ABC" | sed "s/$pattern/g"
        sed: -e expression #1, char 16: unterminated `s' command
        $ echo "123 ABC" | sed "s/$pattern//g"
        123ABC
        $ echo "123 ABC" | sed "s/$pattern/XYZ/g"
        123XYZABC


        And...




        Shell variables are assigned without a leading $.







        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          $ pattern='[^a-zA-Z0-9]'
          $ echo "123 ABC" | sed "s/$pattern/g"
          sed: -e expression #1, char 16: unterminated `s' command
          $ echo "123 ABC" | sed "s/$pattern//g"
          123ABC
          $ echo "123 ABC" | sed "s/$pattern/XYZ/g"
          123XYZABC


          And...




          Shell variables are assigned without a leading $.







          share|improve this answer












          $ pattern='[^a-zA-Z0-9]'
          $ echo "123 ABC" | sed "s/$pattern/g"
          sed: -e expression #1, char 16: unterminated `s' command
          $ echo "123 ABC" | sed "s/$pattern//g"
          123ABC
          $ echo "123 ABC" | sed "s/$pattern/XYZ/g"
          123XYZABC


          And...




          Shell variables are assigned without a leading $.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 7 '17 at 16:57









          FaxMax

          428219




          428219






















              up vote
              1
              down vote













              Replace



              $pattern="[^a-zA-Z0-9]"


              by



              pattern="[^a-zA-Z0-9]"


              Shell variables are assigned without a leading $.






              share|improve this answer
























                up vote
                1
                down vote













                Replace



                $pattern="[^a-zA-Z0-9]"


                by



                pattern="[^a-zA-Z0-9]"


                Shell variables are assigned without a leading $.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Replace



                  $pattern="[^a-zA-Z0-9]"


                  by



                  pattern="[^a-zA-Z0-9]"


                  Shell variables are assigned without a leading $.






                  share|improve this answer












                  Replace



                  $pattern="[^a-zA-Z0-9]"


                  by



                  pattern="[^a-zA-Z0-9]"


                  Shell variables are assigned without a leading $.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 7 '17 at 16:55









                  Patrick Mevzek

                  2,0381721




                  2,0381721



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409521%2fhow-can-i-use-a-shell-variable-to-copy-an-sed-pattern%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)