Sed: find and replace backwards, until string

Some help please:
Need to find string ||(everything in front of it)B0300|| and replace it with ||0|| globally

In:
16112121||||0||0||0||0||0||52||52||0||0||0||0||1507200053342B0300||1507200053342B0300||0||0||0||0700

Out:
16112121||||0||0||0||0||0||52||52||0||0||0||0||0||0||0||0||0||0700

1507200053342 may differ each time.

sed 's/dont know how to do this:-)B0300||/||0||/g'

Thank you in advance for any help

Hello drbiloukos,

Please use code tags as per forum rules for inputs/codes which you are using for your posts. Could you please use following and let me know if this helps you.

 echo "16112121||||0||0||0||0||0||52||52||0||0||0||0||1507200053342B0300||1507200053342B0300||0||0||0||0700" | awk -F"|" '{for(i=1;i<=NF;i++){if($i ~ /B0300/){$i=0};A=A?A OFS $i:$i}} END{print A}' OFS="|"
 

Output will be as follows.

 16112121||||0||0||0||0||0||52||52||0||0||0||0||0||0||0||0||0||0700
 

Thanks,
R. Singh

1 Like

Apologies I forgot tags. It works as expected. Many thanks.

sed 's#\([^|]*B0300\)\([|]\)#0\2#g' filename
1 Like
awk 'gsub(/[^|]*B0300\|/,"0|")' file
1 Like