-1

How can I join all lines together without missing the last elements.

zip() seems to be missing the last elements in this stream.

For example: seq 0 70 | xargs -L 50 | sed 's/ /,/g'

Input:

seq 0 70 | python -c $'import sys\nfor x in zip(*[iter(sys.stdin)]*50): print(",".join(x).replace("\\n",""))\n'

Wrong Output:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49

Expected 50 items per line and last zip contains the rest:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69

Related:

Community
  • 1
  • 1
jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • 1
    `zip` stops on the shortest iterable. An option is to use `izip_longest` with `None`s padded for the shorter iterables (or pass a `fillvalue` to override `None`). A better alternative however is to use a better grouper recipe. With some modification, this may help: http://stackoverflow.com/a/40063403/3125566 – Moses Koledoye Oct 24 '16 at 22:24
  • @MosesKoledoye how does that work between unix pipes, and as a unix one liner? – jmunsch Oct 24 '16 at 22:32

1 Answers1

1
 $'import sys,itertools\nfor x in itertools.izip_longest(*[iter(sys.stdin)]*50): print(",".join(x).replace("\\n",""))\n'

maybe? this uses izip_longest to not drop extra items at the end

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179