删除最后匹配的单词

时间:2016-11-04 14:46:50

标签: shell

我想在条件

之后删除所有字符串

我的输入是

DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE T1.a=T2.b)

希望输出为

DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE

我用sed命令尝试了

output=`echo DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2
WHERE T1.a=T2.b) | sed -n -e 's/[Ww][Hh][Ee][Rr][Ee].*//p'`

但是输出为

  

从abc T1删除

1 个答案:

答案 0 :(得分:0)

使用sed,使用BRE:

sed 's/\(.*WHERE\).*/\1/;s/\(.*where\).*/\1/;' <<< "DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE T1.a=T2.b)"

使用GNU sed,使用i(不区分大小写)修饰符:

sed 's/\(.*where\).*/\1/i' <<< "DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE T1.a=T2.b)"

或替换|运算符:

sed -r 's/(.*(where|WHERE)).*/\1/i' <<< "DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE T1.a=T2.b)"

<强>输出:

DELETE FROM abc T1 WHERE EXISTS (SELECT 1 FROM cdef T2 WHERE