Is there a way to replace last occurrence of match using a shell variable substitution?

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











up vote
2
down vote

favorite
1












% x=abracadabra
% echo $x//a/o
obrocodobro


Hmph...



Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed, awk, perl, etc.)?







share|improve this question




















  • Nope there isn't
    – llua
    Feb 27 at 2:41










  • @llua: dang ...
    – kjo
    Feb 27 at 2:44














up vote
2
down vote

favorite
1












% x=abracadabra
% echo $x//a/o
obrocodobro


Hmph...



Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed, awk, perl, etc.)?







share|improve this question




















  • Nope there isn't
    – llua
    Feb 27 at 2:41










  • @llua: dang ...
    – kjo
    Feb 27 at 2:44












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





% x=abracadabra
% echo $x//a/o
obrocodobro


Hmph...



Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed, awk, perl, etc.)?







share|improve this question












% x=abracadabra
% echo $x//a/o
obrocodobro


Hmph...



Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed, awk, perl, etc.)?









share|improve this question











share|improve this question




share|improve this question










asked Feb 27 at 2:39









kjo

3,84873358




3,84873358











  • Nope there isn't
    – llua
    Feb 27 at 2:41










  • @llua: dang ...
    – kjo
    Feb 27 at 2:44
















  • Nope there isn't
    – llua
    Feb 27 at 2:41










  • @llua: dang ...
    – kjo
    Feb 27 at 2:44















Nope there isn't
– llua
Feb 27 at 2:41




Nope there isn't
– llua
Feb 27 at 2:41












@llua: dang ...
– kjo
Feb 27 at 2:44




@llua: dang ...
– kjo
Feb 27 at 2:44










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"



From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:




$parameter/pattern/string 


The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with ‘/’, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with ‘#’, it must match at the beginning of the expanded value of
parameter. If pattern begins with ‘%’, it must match at the end of the
expanded value of parameter.
If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is ‘@’ or ‘’, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with ‘@’ or ‘
’, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.




So



$ x=abracadabra
$ echo "$x/%a/o"
abracadabro





share|improve this answer



























    up vote
    1
    down vote













    You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:



     x=abracadabra
    echo "$x%a*o$x##*a"


    EDIT: Or just do what steeldriver suggested in the comments.






    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%2f426831%2fis-there-a-way-to-replace-last-occurrence-of-match-using-a-shell-variable-substi%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
      3
      down vote



      accepted










      Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"



      From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
      :




      $parameter/pattern/string 


      The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
      longest match of pattern against its value is replaced with string. If
      pattern begins with ‘/’, all matches of pattern are replaced with
      string. Normally only the first match is replaced. If pattern begins
      with ‘#’, it must match at the beginning of the expanded value of
      parameter. If pattern begins with ‘%’, it must match at the end of the
      expanded value of parameter.
      If string is null, matches of pattern are
      deleted and the / following pattern may be omitted. If the nocasematch
      shell option (see the description of shopt in The Shopt Builtin) is
      enabled, the match is performed without regard to the case of
      alphabetic characters. If parameter is ‘@’ or ‘’, the substitution
      operation is applied to each positional parameter in turn, and the
      expansion is the resultant list. If parameter is an array variable
      subscripted with ‘@’ or ‘
      ’, the substitution operation is applied to
      each member of the array in turn, and the expansion is the resultant
      list.




      So



      $ x=abracadabra
      $ echo "$x/%a/o"
      abracadabro





      share|improve this answer
























        up vote
        3
        down vote



        accepted










        Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"



        From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
        :




        $parameter/pattern/string 


        The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
        longest match of pattern against its value is replaced with string. If
        pattern begins with ‘/’, all matches of pattern are replaced with
        string. Normally only the first match is replaced. If pattern begins
        with ‘#’, it must match at the beginning of the expanded value of
        parameter. If pattern begins with ‘%’, it must match at the end of the
        expanded value of parameter.
        If string is null, matches of pattern are
        deleted and the / following pattern may be omitted. If the nocasematch
        shell option (see the description of shopt in The Shopt Builtin) is
        enabled, the match is performed without regard to the case of
        alphabetic characters. If parameter is ‘@’ or ‘’, the substitution
        operation is applied to each positional parameter in turn, and the
        expansion is the resultant list. If parameter is an array variable
        subscripted with ‘@’ or ‘
        ’, the substitution operation is applied to
        each member of the array in turn, and the expansion is the resultant
        list.




        So



        $ x=abracadabra
        $ echo "$x/%a/o"
        abracadabro





        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"



          From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
          :




          $parameter/pattern/string 


          The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
          longest match of pattern against its value is replaced with string. If
          pattern begins with ‘/’, all matches of pattern are replaced with
          string. Normally only the first match is replaced. If pattern begins
          with ‘#’, it must match at the beginning of the expanded value of
          parameter. If pattern begins with ‘%’, it must match at the end of the
          expanded value of parameter.
          If string is null, matches of pattern are
          deleted and the / following pattern may be omitted. If the nocasematch
          shell option (see the description of shopt in The Shopt Builtin) is
          enabled, the match is performed without regard to the case of
          alphabetic characters. If parameter is ‘@’ or ‘’, the substitution
          operation is applied to each positional parameter in turn, and the
          expansion is the resultant list. If parameter is an array variable
          subscripted with ‘@’ or ‘
          ’, the substitution operation is applied to
          each member of the array in turn, and the expansion is the resultant
          list.




          So



          $ x=abracadabra
          $ echo "$x/%a/o"
          abracadabro





          share|improve this answer












          Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"



          From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
          :




          $parameter/pattern/string 


          The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
          longest match of pattern against its value is replaced with string. If
          pattern begins with ‘/’, all matches of pattern are replaced with
          string. Normally only the first match is replaced. If pattern begins
          with ‘#’, it must match at the beginning of the expanded value of
          parameter. If pattern begins with ‘%’, it must match at the end of the
          expanded value of parameter.
          If string is null, matches of pattern are
          deleted and the / following pattern may be omitted. If the nocasematch
          shell option (see the description of shopt in The Shopt Builtin) is
          enabled, the match is performed without regard to the case of
          alphabetic characters. If parameter is ‘@’ or ‘’, the substitution
          operation is applied to each positional parameter in turn, and the
          expansion is the resultant list. If parameter is an array variable
          subscripted with ‘@’ or ‘
          ’, the substitution operation is applied to
          each member of the array in turn, and the expansion is the resultant
          list.




          So



          $ x=abracadabra
          $ echo "$x/%a/o"
          abracadabro






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 27 at 2:49









          steeldriver

          31.5k34978




          31.5k34978






















              up vote
              1
              down vote













              You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:



               x=abracadabra
              echo "$x%a*o$x##*a"


              EDIT: Or just do what steeldriver suggested in the comments.






              share|improve this answer
























                up vote
                1
                down vote













                You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:



                 x=abracadabra
                echo "$x%a*o$x##*a"


                EDIT: Or just do what steeldriver suggested in the comments.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:



                   x=abracadabra
                  echo "$x%a*o$x##*a"


                  EDIT: Or just do what steeldriver suggested in the comments.






                  share|improve this answer












                  You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:



                   x=abracadabra
                  echo "$x%a*o$x##*a"


                  EDIT: Or just do what steeldriver suggested in the comments.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 27 at 2:46









                  Captain Wobbles

                  1565




                  1565






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426831%2fis-there-a-way-to-replace-last-occurrence-of-match-using-a-shell-variable-substi%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay