Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to replace optional underscore

Tags:

regex

linux

shell

I need to strip an underscore optionally if it exists.

string VIEW VXABC expected result VIEW ABC
string VIEW V_XABC expected result VIEW ABC

This is my try but results are eluding me

echo "VIEW VXABC" |sed 's/VIEW V_?X/VIEW ABC/'
VIEW VXABC
like image 864
jhon.smith Avatar asked Jan 30 '26 20:01

jhon.smith


1 Answers

[me@home]$ echo VIEW V_XABC | sed -r 's/VIEW V_?X/VIEW /' 
VIEW ABC
[me@home]$ echo VIEW VXABC | sed -r 's/VIEW V_?X/VIEW /' 
VIEW ABC

Note the -r option. From the man page:

-r, --regexp-extended
    use extended regular expressions in the script.
like image 80
Shawn Chin Avatar answered Feb 02 '26 12:02

Shawn Chin