while read r;do i= s=${r#*ATG} ((${#s}==${#r}))&&s= until [[ ${s:i:3} =~ (TAA|TAG|TGA) ]]||((i+3>${#s}));do ((i+=3));done echo ${s:0:i} done