echo 'aaabbccccdd eee' | perl -aF'(.)(?!\1)' -lne 'while(@F){push@M,shift(@F).shift(@F)}print for @M' #### aaa bb cccc dd eee