PravinCG
PravinCG

Reputation: 7708

Find and Replace first character after a certain pattern

Current text

Variable length text = some string(some more text

Change to

Variable length text = some string(addition some more text

Need to add a certain text after first parenthesis in a line only after "=" character is encountered. Another condition is to ignore patterns like "= (", which essentially means you should ignore patterns with only space between "=" and "("

My Try:

sed -e "s#*=(\w\()#\1(addition#g"

Thanks in anticipation!

Upvotes: 0

Views: 611

Answers (2)

Lev Levitsky
Lev Levitsky

Reputation: 65781

Tweak this for your needs:

$ echo 'Variable length text = some string(some more text' |\
sed 's/^[^=]*=[^(]*[[:alnum:]][^(]*(/&addition /'

That matches for:

  1. Beginning of the string
  2. Anything but = any number of times
  3. =
  4. Anything but ( any number of times
  5. An alpha-numeric character
  6. Anything but ( any number of times
  7. (

... and substitutes it with the matched string adding ' addition' to it. The output is

Variable length text = some string(addition some more text

Upvotes: 1

pulven
pulven

Reputation: 156

in perl

s/(.*?=[\s][^(]+?)\((.*)/$1(aditional text $2/

Upvotes: 1

Related Questions