#!/bin/bash s='AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012__386___Day_' echo "$s" sed -E 's/([^_]|^)__([^_]|$)/\1.abc.def__\2/g' <<< "$s" perl -i -pe 's/(?<!_)__(?!_)/.abc.def__/g' <<< "$s"
Standard input is empty
AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012__386___Day_ AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012.abc.def__386___Day_ AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012.abc.def__386___Day_
-i used with no filenames on the command line, reading from STDIN.