Sen
Sen

Reputation: 79

Replace random string starting and ending using shell command

Appreciate any help to find out the solution for below scenario.

Input:

:50Kfoobar:53B:53B:53Bbaz:53B:53B;:50Kvoocar:53B:53B:53Bbaz:53B:53B

Output:

:50KCREDIT:53B:53B:53Bbaz:53B:53B;:50KCREDIT:53B:53B:53Bbaz:53B:53B

Tried using below command

sed 's/:50K.*:53B/:50KCREDIT:53B/g' <<<':50Kfoobar:53B:53B:53Bbaz:53B:53B;:50Kvoocar:53B:53B:53Bbaz:53B:53B'

producing below result
:50KCREDIT:53B

Upvotes: 1

Views: 242

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133538

With your shown samples, please try following.

awk '{gsub(/:50K[^:]*:53B/,":50KCREDIT:53B")} 1' Input_file

Explanation: using regex :50K[^:]*:53B which will match :50K till next occurrence of :(non-greedy match) then matching : followed by 53B, then replacing this matched part with :50KCREDIT:53B globally, finally printing the current line.

Upvotes: 3

Related Questions