#!/bin/sed -f # Concatenates up to 6 lines with ' ' # #i.e.: '1\n2\n3\n4\n5\n\6\n7\n\8\n9\n10\n11\n12\n13\n14\n' # becomes '1 2 3 4 5 6\n7 8 9 10 11 12\n13 14\n' : restart # if less than 5 embedded newlines, i.e. < 6 fields in pattern space /\(.*\n\)\{5\}/ ! { # if last line, print $ b print # otherwise, append next line to pattern space and repeat N b restart } # 6 fields in pattern or all available, even when less : print # replace embedded line ends with desired field separator y/\n/ / # then end cycle with output