Find and Replace with sed

Hi,

I have a file such that:

tart*)*98'bank'ksb64bank)(tart2d&f44bank

I want to replace to: (only between tart and bank)

tart*)*98'replaced'ksb64bank)(tart2d&f44replaced

Thanks.

Please show exact desired output and clarify your request. There are 2 tarts and 3 banks?

Yes it's correct. The process would be done only between a tart and bank.
not for other bank.

tart*)*98'replaced'ksb64no replace)(tart2d&f44replaced

The last bank is not between tart and bank so what is the crireria to replace it?

Hi,
it is also the same. Search tart*.*bank and change bank to "replaced"

tart*)*98'bank'ksb64bank)(tart2d&f44bank

Try:

perl -pe 's/(tart.*?)bank/$1replaced/g'
sed 's/bank/�/g; s/\(tart[^�]*\)�/\1replaced/g; s/�/bank/g' 

That is not the way regular expressions work in sed (or awk, ed, ex, or vi). The t*.* in tart*.*bank matches everything in red in the line:

tart*)*98'bank'ksb64bank)(tart2d&f44bank

Note that the RE you provided will also match tarxyzbank (with no "t" after "tar").

The following sed script seems to do what you want:

sed 's/bank/^Gbank/g
s/\(tart[^^G]*\)^Gbank/\1replaced/g
s/^G//g' input

Note that each time the sequence ^G appears in this script it needs to be replaced by a single character that can never occur in your input. I used an alert character (CTRL-G in ASCII), but you can use whatever you want in the above sed commands except for the <asterisk> ( * ), <backslash> ( \ ), <circumflex> ( ^ ), <dollar-sign> ( $ ), <left-square-bracket> ( [ ), <period> ( . ) and <slash> ( / ) characters.