I have a file test.txt on my linux system which has data in following format :
first second third fourth 10 first second third fourth 20 fifth sixth sevent
Here is one way to do it:
awk ' { last=$NF; $NF="" if($0==previous) { tail=tail " " last } else { if(previous!="") { if(split(tail,foo)==1) tail=tail " 0" print previous tail } previous=$0 tail=last } } END { if(previous!="") print previous tail } '