csh - Parsing a string with sed -


i have string prefix-2020.80-suffix-1 here of possible combinations of input string

"2020.80-suffix-1" "2020.80-suffix" "prefix-2020.80" "prefix-2020.80-1" 

i need cut out , assign 2020 variable cannot desired output

here got far...

set var=`echo "prefix-2020.80-suffix-1" | sed "s/[[:alnum:]]*-*\([0-9]*\).*/\1/"` 

my regexp not work other cases , cannot figure out why! more complicated python's regexp syntax

this should work inputs

sed 's/.*\(^\|-\)\([0-9]*\)\..*/\2/' test 

matches start of line or -[number]. , captures number.

the problem original using didn't take account when there wasn't prefix.


Comments