Issue
Hope you are doing.
I need help in regex in the shell script.
the input is:
PM_path='SubNetwork=IMS,SubNetwork=IMS,MeContext=R9NEXRERNFVCSS01_PS'
**PM_path='SubNetwork=IMS,SubNetwork=IMS,MeContext=R9NEXRERNFVCSS01_PS'
the output I want as:
R9NEXRERNFVCSS01_PS that is everything after the last equal sign.
Right now I have implemented it as below:
if [[ $PM_path =~ MeContext=([a-zA-Z0-9_]+) ]]; then
NODE_NAME=${BASH_REMATCH[1]}
echo "the value is matched";
So here I have put a check at MeContext and this is a part of PM_path.
I wanted to make it more generic like select everything which appears after the last equal sign.
Please help.
Thanks in advance.
Solution
I wanted to make it more generic like select everything which appears after the last equal sign.
You may use:
[[ $PM_path =~ .*=([^/]+) ]] && echo "${BASH_REMATCH[1]}"
R9NEXRERNFVCSS01_PS
.*
matches longest possible text from start then we match a =
. Finally we match and capture remaining string of 1+ non-/
characters in ([^/]+)
that we print using echo "${BASH_REMATCH[1]}"
Answered By - anubhava