regex to replace optional underscore

linux, regex, shell

Solution

[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.

Problem

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 ```

Original source