Regular expressions replace optional underscores

Regular expressions replace optional underscores … here is a solution to the problem.

Regular expressions replace optional underscores

If the underscore is present, I need to remove it.

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

This is my attempt, but I don’t know the result

echo "VIEW VXABC" |sed 's/VIEW V_? X/VIEW ABC/'
VIEW VXABC

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. fromman page :

-r, --regexp-extended
    use extended regular expressions in the script.

Related Problems and Solutions