Tuesday, 27 August 2013

Rearrange data into specified row lengths

Rearrange data into specified row lengths

I have a data file that is just a long list of numbers, with each entry on
a separate line:
23
28
26
14
...
I need to rearrange the data in rows 37 records/fields long:
23 28 26 14 1 2 3 4 5 6 7 8 9 2 4 5 6 9 4 8 7 6 3 2 5 9 4 1 2 5 7 8 9 4 6 1 2
5 8 6 4 3 5 23 28 26 14 1 2 3 4 5 6 7 8 9 2 4 5 6 9 4 8 7 6 3 2 5 9 4 1 2
5 7
...
This is the code I've tried:
awk '{
for(i=1;i<=NF;i+=1) {
if(i%37 != 0) printf $i" ";
else printf "$i\n"
}
}'
input.txt > output.txt
The first printf $i" " seems to be working, but somethings seems to be
wrong with the conditional because no matter what I tell it to print in
the else statement it doesn't print it. Perhaps just a syntax oversight??
Interestingly, when I just run:
awk '{for(i=1;i<=NF;i+=1) printf $i" "}' input.txt > output.txt
the resultant file puts some of the data into 37-record-length rows, but
some are still much longer...could this be a result of some artifact in
the data? (The data has been run through a number of sorting/organizing
functions.)

No comments:

Post a Comment