Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 1344 rows of data column. I want to form 32 row by 42 columns from my input data file. I want to cut the first 42 row and paste to one raw of 42 column data and continue down likewise. My data is arranged as

file name: data.txt

1
2
3
.
.
.
1344

I want the result be output.txt

1 2 3 ....42
43 44      84
.
.
.
1303 1304 . . . 1344

I want you help me with a script that can do it.

share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

These should work:

awk '{if(n==41){n=0;print $0}else{printf "%s ",$0;n++}}' data.txt 

or

awk '{if(NR % 42 == 0){print;}else{printf "%s ",$0}}' data.txt

or

perl -ne 'if($. % 42){chomp; print "$_ "}else{ print;}' data.txt 

or

perl -pne 's/\n/ / if $. % 42' data.txt
share|improve this answer
    
Thank you very much!! It does my work!! –  Abraham Jan 1 at 18:24
    
@Abraham actually, there was a bug in the original version that would cause you to skip some lines. Please use the updated version. –  terdon Jan 2 at 2:46
add comment

How about this:

COUNT=0
while read data
do
  echo -n "${data} "
  let COUNT=${COUNT}+1;
  if (( ${COUNT} % 42  == 0 ))
  then
    echo ""
  fi
done < data.txt
share|improve this answer
    
Perfect!! Thank you very much!! –  Abraham Jan 1 at 18:22
add comment

If you could use python, numpy is quite well suited for this. Assuming a file x.txt contains the data, you could do something like:

#!/usr/bin/env python
import numpy as np

a=np.loadtxt('x.txt', dtype='int') # load array from file x.txt into variable a
print a
b=a.reshape(32,42) # reshape array to desired shape
print b
c=a.T # Do a transpose if required
print c
share|improve this answer
add comment

Using paste

Only because I love using paste so much, you can actually do this with paste, though it isn't pretty:

Sample data

$ seq 100 > data.txt

Example

$ paste -d " " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - < data.txt
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100     

If you need to control the number of columns, then add/remove the corresponding amount of dashes from paste.

You can get fancy and use a loop to control the generation of the dashes to paste like so:

$ paste -d " " $(for i in $(seq 42); do echo "- ";done) < data.txt 
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    

Further enhancements can be made to the loop. Instead of using seq we can use the shells brace expansion {#...#}.

$ paste -d " " $(for i in {1..42}; do echo "- ";done) < data.txt

Using xargs

Another less obvious method would be to make xargs do the controlling of the number of characters through its -n # switch.

$ seq 100 | xargs -n 42
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

So we could leverage this method like so:

$ xargs -n 42 < data.txt 
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

These methods will only work when the data we're dealing with is space delimited. Also of note with the xargs method, this is by default, just echoing out the arguments, but it could be modified so that it could perform an action for each of the arguments as well.

share|improve this answer
    
The xargs one is brilliant! –  terdon Jan 2 at 2:23
    
@terdon - thanks, these questions are always fun, since we can figure out as many ways as possible to do X. I was going to do it Perl but saw you beaten me to it so I figured I try other ways. –  slm Jan 2 at 2:33
add comment

The easiest way is to use rs command - if it is available:

cat original.file | rs 32 42 > reshaped.file
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.