from a variable detect the last occurrence of two letters (first letter is C and second letter can be A or B) and apply some delete and if condition

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











up vote
0
down vote

favorite












Consider below variable
letters="1234, MR45, MB46, 1234"
Need a command to detect last occurrence of M[B or R] where M is fixed and second letter can be B or R and delete everything before it
Expected output
output="MB46, 1234"
Also need if command to print error if letters variable doesnot have MB or MR word







share|improve this question
























    up vote
    0
    down vote

    favorite












    Consider below variable
    letters="1234, MR45, MB46, 1234"
    Need a command to detect last occurrence of M[B or R] where M is fixed and second letter can be B or R and delete everything before it
    Expected output
    output="MB46, 1234"
    Also need if command to print error if letters variable doesnot have MB or MR word







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Consider below variable
      letters="1234, MR45, MB46, 1234"
      Need a command to detect last occurrence of M[B or R] where M is fixed and second letter can be B or R and delete everything before it
      Expected output
      output="MB46, 1234"
      Also need if command to print error if letters variable doesnot have MB or MR word







      share|improve this question












      Consider below variable
      letters="1234, MR45, MB46, 1234"
      Need a command to detect last occurrence of M[B or R] where M is fixed and second letter can be B or R and delete everything before it
      Expected output
      output="MB46, 1234"
      Also need if command to print error if letters variable doesnot have MB or MR word









      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 15 '17 at 18:20









      user8554534

      3516




      3516




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Bash solution (regex matching):



          letters="1234, MR45, MB46, 1234"

          if [[ "$letters" =~ .*(M[BR].*) ]]; then
          echo "$BASH_REMATCH[1]" # MB46, 1234
          else
          echo "MB or MR word not found!"
          fi



          As a simplified shortened alternative the following GNU grep approach may be used:



          grep -Po '.*KM[BR].*' <<<"$letters" || echo "MB or MR word not found"





          share|improve this answer






















          • thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
            – user8554534
            Oct 15 '17 at 18:50










          • there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
            – RomanPerekhrest
            Oct 15 '17 at 19:05











          • A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
            – Arrow
            Oct 15 '17 at 22:52











          • Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
            – Arrow
            Oct 15 '17 at 22:56










          • @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
            – RomanPerekhrest
            Oct 16 '17 at 6:51










          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%2f398264%2ffrom-a-variable-detect-the-last-occurrence-of-two-letters-first-letter-is-c-and%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Bash solution (regex matching):



          letters="1234, MR45, MB46, 1234"

          if [[ "$letters" =~ .*(M[BR].*) ]]; then
          echo "$BASH_REMATCH[1]" # MB46, 1234
          else
          echo "MB or MR word not found!"
          fi



          As a simplified shortened alternative the following GNU grep approach may be used:



          grep -Po '.*KM[BR].*' <<<"$letters" || echo "MB or MR word not found"





          share|improve this answer






















          • thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
            – user8554534
            Oct 15 '17 at 18:50










          • there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
            – RomanPerekhrest
            Oct 15 '17 at 19:05











          • A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
            – Arrow
            Oct 15 '17 at 22:52











          • Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
            – Arrow
            Oct 15 '17 at 22:56










          • @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
            – RomanPerekhrest
            Oct 16 '17 at 6:51














          up vote
          0
          down vote



          accepted










          Bash solution (regex matching):



          letters="1234, MR45, MB46, 1234"

          if [[ "$letters" =~ .*(M[BR].*) ]]; then
          echo "$BASH_REMATCH[1]" # MB46, 1234
          else
          echo "MB or MR word not found!"
          fi



          As a simplified shortened alternative the following GNU grep approach may be used:



          grep -Po '.*KM[BR].*' <<<"$letters" || echo "MB or MR word not found"





          share|improve this answer






















          • thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
            – user8554534
            Oct 15 '17 at 18:50










          • there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
            – RomanPerekhrest
            Oct 15 '17 at 19:05











          • A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
            – Arrow
            Oct 15 '17 at 22:52











          • Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
            – Arrow
            Oct 15 '17 at 22:56










          • @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
            – RomanPerekhrest
            Oct 16 '17 at 6:51












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Bash solution (regex matching):



          letters="1234, MR45, MB46, 1234"

          if [[ "$letters" =~ .*(M[BR].*) ]]; then
          echo "$BASH_REMATCH[1]" # MB46, 1234
          else
          echo "MB or MR word not found!"
          fi



          As a simplified shortened alternative the following GNU grep approach may be used:



          grep -Po '.*KM[BR].*' <<<"$letters" || echo "MB or MR word not found"





          share|improve this answer














          Bash solution (regex matching):



          letters="1234, MR45, MB46, 1234"

          if [[ "$letters" =~ .*(M[BR].*) ]]; then
          echo "$BASH_REMATCH[1]" # MB46, 1234
          else
          echo "MB or MR word not found!"
          fi



          As a simplified shortened alternative the following GNU grep approach may be used:



          grep -Po '.*KM[BR].*' <<<"$letters" || echo "MB or MR word not found"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 16 '17 at 6:46

























          answered Oct 15 '17 at 18:28









          RomanPerekhrest

          22.5k12145




          22.5k12145











          • thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
            – user8554534
            Oct 15 '17 at 18:50










          • there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
            – RomanPerekhrest
            Oct 15 '17 at 19:05











          • A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
            – Arrow
            Oct 15 '17 at 22:52











          • Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
            – Arrow
            Oct 15 '17 at 22:56










          • @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
            – RomanPerekhrest
            Oct 16 '17 at 6:51
















          • thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
            – user8554534
            Oct 15 '17 at 18:50










          • there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
            – RomanPerekhrest
            Oct 15 '17 at 19:05











          • A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
            – Arrow
            Oct 15 '17 at 22:52











          • Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
            – Arrow
            Oct 15 '17 at 22:56










          • @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
            – RomanPerekhrest
            Oct 16 '17 at 6:51















          thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
          – user8554534
          Oct 15 '17 at 18:50




          thanks a lot it works fine...in the command sed 's/.*(M[BR].*)/1/' how to detect the last ocuurence of the word MB or MR from variable even if the letters are in lower case
          – user8554534
          Oct 15 '17 at 18:50












          there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
          – RomanPerekhrest
          Oct 15 '17 at 19:05





          there's no sed solution now. I've updated my solution to fit your additional condition to print error if letters variable doesnot have MB or MR word. If you need case-insensitive search - you should mention that in your question
          – RomanPerekhrest
          Oct 15 '17 at 19:05













          A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
          – Arrow
          Oct 15 '17 at 22:52





          A bash/sed solution: sed -E 's/.*M([BR])/M1/1' <<<"$letters" maybe? (add an if to it).
          – Arrow
          Oct 15 '17 at 22:52













          Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
          – Arrow
          Oct 15 '17 at 22:56




          Using if [[ "$letters" =~ .*(M[BR].*) ]]; then echo "$BASH_REMATCH[1]" seems better IMvhO.
          – Arrow
          Oct 15 '17 at 22:56












          @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
          – RomanPerekhrest
          Oct 16 '17 at 6:51




          @Arrow, as for .*(M[BR].*) - good hint, I've missed that. Thanks
          – RomanPerekhrest
          Oct 16 '17 at 6:51

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398264%2ffrom-a-variable-detect-the-last-occurrence-of-two-letters-first-letter-is-c-and%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?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?