Issue
I want to extract an atomic symbols inside a parentheses using sed.
The data I have is in the form C(X12)
, and I only want the X symbol
echo "C(Br12)" | sed 's/[0-9][0-9])$//g'
gives me C(Br
.
Solution
You can use
sed -n 's/.*(\(.*\)[0-9]\{2\})$/\1/p'
See the online demo:
sed -n 's/.*(\(.*\)[0-9]\{2\})$/\1/p' <<< "c(Br12)"
# => Br
Details
-n
- suppresses the default line output.*(\(.*\)[0-9]\{2\})$
- a regex that matches.*
- any text(
- a(
char\(.*\)
- Capturing group 1: any text up to the last....[0-9]\{2\}
- two digits)$
- a)
at the end of string
\1
- replaces with Group 1 valuep
- prints the result of the substitution.
Answered By - Wiktor Stribiżew Answer Checked By - Robin (WPSolving Admin)