Amr Elhosiny
Amr Elhosiny

Reputation: 257

Remove string with fixed & variable part

I have the following string

.VBP ( <variable string> )

I want to match the pattern using .VBP and remove the entire pattern above. How can I do it using sed ? Note that the above pattern exist in the same line with other patterns. I want to just remove this pattern and leave the entire line untouched.

Upvotes: 0

Views: 59

Answers (1)

riteshtch
riteshtch

Reputation: 8769

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

Edit: since you provided a sample text. It doesnt have spaces as you mentioned in your question. Here is the modified version:

$ 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));
$

Here is the explanantion for sed -r 's/^(.*),\.VBP\s*\([^\)]*\)(.*)/\1\2/g'

-r switch enables Extended Regular Expressions (ERE)
s is the sed command for substituting a regex match with a replacement string

^                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  

substituting with group 1 and 2 gives the string before and after pattern.

Upvotes: 1

Related Questions