sed ':a;N;$!ba;s/ / /g' file :a create a label 'a' N append the next line to the pattern space $! if not the last line, ba branch (go to) label 'a' s substitute, / / regex for new line, / / by a space, /g global match (as many times as it can)