I have a file looks like below:1 4 5 6 7 19202224 26 27 29303132 34 40 50 56 58100234 235 270 5001234 1235 1236 123723002303230423072309As it is clear there are some rows with more than 1 column and some others with only one single column. I'd like to join together single-column rows  such that there are at most 4 columns on each combined row. So the output should look like this:1  4 5 6 7 1920 2224 26 27 29 30 31 3234 40 50 56 58 100234 235 270 5001234 1235 1236 12372300 2303 2304 23072309Any suggestions on how do this, considering that the real data is big?
How to join rows with single columns to a maximum of 4 columns in one row?
text processing;awk;perl
A little bit idiomatic but working with gnu awk:awk '{printf %s,(NF==1?$0 FS:(c==0?:RS) $0 RS)} \{(NF==1?++c:c=0)} \c==4{printf \n;c=0} \END{printf \n}' file#Output1 4 5 6 7 1920 22 24 26 2729 30 31 32 34 40 50 56 58 100 234 235 270 5001234 1235 1236 12372300 2303 2304 2307 2309 Explanation:awk variables:NF=Number of FieldsFS=Field Separator = space by defaultRS=Record Separator= new line by default.c=counter  Line1: {printf %s,(NF==1?$0 FS:(c==0?:RS) $0 RS)}: nested ternary if operations    #Single ternary if operation:condition?true action:false action#Nested if operations:  condition1?true action 1:(condition2:true action2:false action2) #nested ternary if operations   -------------------------[            ^ false action1 ^        ]   This can be explained in pseudocode like:   if NF==1 then print $0 and print FS   else (if c==0 then print  else print RS) and print $0 and print RS again   Line 2: {(NF==1?++c:c=0)} : Another ternary if operation that can be expressed as: If NF==1 (line has one field) then increase counter c by one else reset counter c.  Line 3 : c==4{printf \n;c=0} Classic awk syntax : condition{action} If counter c==4 then print a new line and reset counter cLine 4: END{printf \n}' file : This justs prints a new line at the end of the script.