删除字符串固定&可变部分

时间:2016-03-01 09:26:40

标签: sed

我有以下字符串

.VBP ( <variable string> )

我想使用.VBP匹配模式并删除上面的整个模式。我怎么能用sed做呢? 请注意,上述模式与其他模式存在于同一行中。我想删除这个模式并保持整条线不受影响。

1 个答案:

答案 0 :(得分:1)

$ sed -r 's/^(.*)\.VBP\s\([^\)]*\)(.*)/\1\2/g'
text before .VBP (randomtextrandomtextfndljknhd) text after
text before  text after
texttext .VBP (abcd) text
texttext  text
$

编辑:因为您提供了示例文本。它没有您在问题中提到的空格。这是修改后的版本:

$ sed -r 's/^(.*)\.VBP\s*\([^\)]*\)(.*)/\1\2/g'
SEN_BUF_S_1 pnr_postcts1(.X(n1),.A(sync_ff0_s),.VSS(vssd_digstby) ,.VDD(vddd1v_dig),.VBN(vssd_digstby),.VBP(vddd1v_dig));
SEN_BUF_S_1 pnr_postcts1(.X(n1),.A(sync_ff0_s),.VSS(vssd_digstby) ,.VDD(vddd1v_dig),.VBN(vssd_digstby),);
$ sed -r 's/^(.*),\.VBP\s*\([^\)]*\)(.*)/\1\2/g'
SEN_BUF_S_1 pnr_postcts1(.X(n1),.A(sync_ff0_s),.VSS(vssd_digstby) ,.VDD(vddd1v_dig),.VBN(vssd_digstby),.VBP(vddd1v_dig));
SEN_BUF_S_1 pnr_postcts1(.X(n1),.A(sync_ff0_s),.VSS(vssd_digstby) ,.VDD(vddd1v_dig),.VBN(vssd_digstby));
$

这是解释的解释 sed -r 's/^(.*),\.VBP\s*\([^\)]*\)(.*)/\1\2/g'

-r开关启用扩展正则表达式(ERE)
s是使用替换字符串替换正则表达式匹配的sed命令

^                matches start of line anchor
(.*),            matches any char (.) 0 or more times(*)(group 1) followed by a literal comma
\.VBP            matches a literal dot and VBP
\s*              \s stands for white-space (tab, spaces etc), match it 0 or more times
\(              match a literal opening round bracket
[^\)]*          matches any character i.e not a closing bracket 0 or more times
\)              match a literal closing round bracket
(.*)            match the rest of the line (any char 0 or more times)=group2  

用组1和2代替,给出模式前后的字符串。