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

Clash 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
linux awk sed grep cut
add a comment |Â
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
linux awk sed grep cut
add a comment |Â
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
linux awk sed grep cut
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
linux awk sed grep cut
asked Oct 15 '17 at 18:20
user8554534
3516
3516
add a comment |Â
add a comment |Â
 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"
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
Usingif [[ "$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
add a comment |Â
 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"
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
Usingif [[ "$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
add a comment |Â
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"
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
Usingif [[ "$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
add a comment |Â
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"
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"
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
Usingif [[ "$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
add a comment |Â
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
Usingif [[ "$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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password