How can one replace a part of a line with sed?

Viewed 295

I have a file which contains many lines(line delimiter is ~). Each line,I have many elements which is seperated by a delimiter '*'. What I want to do is , I will be having a line that starts with string TRN in my file. It can have 4(including TRN) or more data points in it. Something like,

TRN*1*S521000035*1020494919~
TRN*1*S521000035*1020494919*787989800~

I want to replace the fourth data point from this lines to abc123. ie,

TRN*1*S521000035*abc123~
TRN*1*S521000035*abc123*787989800~

I tried using sed command with regular expression

sed -i 's/^TRN\*(.*)\*(.*)\*(.*)$/abc123/g' file.txt 

But the whole string is getting replaced to abc123.

Is it possible to change only its 4th datapoint using sed command ?

5 Answers
Related