1

I have a request to replace the pattern A.*D with E.*E. however my string have multiple combinations to meet this pattern, like bAcDAccDAccccD. if I just use the normal way to replace, I can't get my expected result, i.e., bEcEEccEEccccE:

echo 'bAcDAccDAccccD'|sed 's/A\(.*\)D/E\1E/g' --> bEcDAccDAccccE.

How to solve such problem?

a4194304
  • 366
  • 1
  • 9

1 Answers1

4

* is greedy quantifier(See Greedy vs. Reluctant vs. Possessive Quantifiers). It will try to match as much as possible

A simple workaround for given case is

$ echo 'bAcDAccDAccccD' | sed 's/A\([^D]*\)D/E\1E/g'
bEcEEccEEccccE

[^D]* will match only non D characters, whereas .* will match any character, including D

Sundeep
  • 19,273
  • 2
  • 19
  • 42