I need to remove everything before and after a name in bash script, the following are examples
test_3123_123_testone-2.cpp
abc_3123_12312_a.cpp
johnchase_4123123123_123123123_johnc-1.cpp
I would need them simply change into
testone.cpp
a.cpp
johnc.cpp
But having trouble with regex and trying to get this setup properly, any advice would be great!
>Solution :
the text right before .cpp but ignoring any dashes(-2) if they exist behind it.
Do exactly that. Write it from the end.
- "before .cpp" -> so
.cppmust be last - "ignoring any dashes" – so there is a dash(-2)
- "if they exist behind it" – the dash is optional
- "the text" – so match the text.
var=test_3123_123_testone-2.cpp
[[ "$var" =~ ([^_-]*)(-[0-9]+)?.cpp$ ]]
echo ${BASH_REMATCH[1]}.cpp