Tuesday, October 25, 2022

[SOLVED] How to parse a substring like "CM-220" from a string using sed in a bash script

Issue

I have a string:

blah\n blah\n blah Refs: CM-220

I know I can extract CM-220 using sed like below

echo 'blah\n blah\n blah Refs: CM-220' | sed 's/.*Refs: \([^ ]*\).*/\1/'

But I want to make it more strictly to extract it as alphabet + "-" + number.


Solution

You may use this sed with ERE mode i.e. -E option:

echo 'blah\n blah\n blah Refs: CM-220' |
sed -E 's/.*Refs: ([[:alpha:]]+-[[:digit:]]+).*/\1/'

CM-220

[[:alpha:]]+-[[:digit:]]+ is translation of your required pattern: alphabet + "-" + number where [[:alpha:]]+ matches 1 or more alphabetic characters and [[:digit:]]+ matches 1 or more digits.



Answered By - anubhava
Answer Checked By - Katrina (WPSolving Volunteer)