我想删除" AS"部分在下面的字符串中,
CONV.BUY AS BUY, CONV.CUSTOM1 AS CUSTOM1 , TP.BUY AS BUY, TP.CUSTOM1 AS CUSTOM1
应改为
CONV.BUY, CONV.CUSTOM1, TP.BUY, TP.CUSTOM1
我做的是
echo $string | sed -e 's/ AS.*\,/\,/g'
给出了
CONV.BUY, TP.CUSTOM1 AS CUSTOM1
在该sed中过滤掉两个字段。有没有更好的解决方案来实现我的目标而不使用循环?
答案 0 :(得分:1)
使用sed
:
sed 's/[[:blank:]]AS[^,]*//g'
[[:blank:]]AS
匹配前面有空格的AS
,然后[^,]*
匹配下一个,
。通过替换为null来删除所有这些匹配的部分。示例:强>
% sed 's/[[:blank:]]AS[^,]*//g' <<<'CONV.BUY AS BUY, CONV.CUSTOM1 AS CUSTOM1 , TP.BUY AS BUY, TP.CUSTOM1 AS CUSTOM1'
CONV.BUY, CONV.CUSTOM1, TP.BUY, TP.CUSTOM1
答案 1 :(得分:1)
您可以使用否定字符类
sed 's/AS[^,]*//g'
[^,]*
匹配零个或多个非,
个字符。这意味着它匹配到直接,
并将其替换为null 示例强>
>>> echo "EONV.BUY AS BUY, CONV.CUSTOM1 AS CUSTOM1 , TP.BUY AS BUY, TP.CUSTOM1 AS CUSTOM1" | gsed 's/AS[^,]*//g'
EONV.BUY , CONV.CUSTOM1 , TP.BUY , TP.CUSTOM1