WITH awk
$ awk -F: 'NF==1 && $0 ~ s{print p ORS $0} {s=$NF; p=$0}' ip.txt
@1:N:0:ABC
ABC
-F:use :as delimiter, makes it easy to get the last columns=$NF; p=$0 save the last column value and the entire row for printing laterNF==1 If the line does not contain :$0 ~ s ,- , , ,
:, , :
GNU sed ( , )
$ sed -nE '/:/{N; /.*:(.*)\n.*\1/p}' ip.txt
@1:N:0:ABC
ABC
/:/ :N/.*:(.*)\n.*\1/ : ,
, , .. ,
@1:N:0:ABC
@1:N:0:XYZ
XYZ