Linux How to cut lines and print in coumns

118 Views Asked by At

I have 100 lines with 1 column. Data like this


line1  
line2  
line3  
.  
.  
.  
line100  

I want output like this


line1     line11     line21 ... line91  
line2     line12     line22 ... line92  
line2     line13     line23 ... line93  
line4     line14     line24 ... line94  
.  
.  
.  
line10    line20     line30 ... line100  

Does anyone help me how to do like this.

Thank you very much.

5

There are 5 best solutions below

0
On

Could you please try following awk too once.

awk -v col=10 '
{
  a[FNR]=$0
}
END{
  for(i=1;i<=col;i++){
     for(j=0;j<=FNR;j=j+col){
        printf("%s%s",a[i+j],j==FNR?"":" ");
}
     print ""
}
}
'   Input_file

Explanation: So here I have created a variable named col whose value is 10(which represents the number of column we wanted to print). Then I am creating an array named a whose index is FNR(current line's value) and value is the current line's value. Now in END block of awk I am starting a for loop which starts from a variable i's value from 1 to till col's value(so maximum till 10 in this case), then started a inner for loop which starts from variable j's value 0 to till value of FNR(which will be total number of lines in Input_file) and each time it gets incremented by value of 10. In side this inner for loop printing the value of array a[i+j], so it will be like a[1],a[11],a[21]..... etc etc Then second string I am printing is space between these array a's values and checking if it is last element please don't print the space then. Now in outer for loop printing null value which will print a new lines here.

0
On

You can use rs:

$ seq 30 | rs 0 4
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  

If you want the lines to go down vs across:

$ seq 30 | rs 4 0 | rs -T
1   9   17  25
2   10  18  26
3   11  19  27
4   12  20  28
5   13  21  29
6   14  22  30
7   15  23  
8   16  24  
0
On

Transposing one column into 10 columns each with 10 values. Sounds like matrix 10x10.
With single gawk script:

awk -v d=10 'NR==1{ c=1 }
     { a[c][++k]=$0; if(!(NR%d)){ c++; k=0 } }
     END{ 
         for(i=1;i<=d;i++) 
             for(j=1;j<=c;j++) printf "%s%s",a[j][i],(j==c)? ORS:FS 
     }' file

  • -d=10 - devisor/coefficient, it allows to specify the maximal number of rows per column

The output:

line1 line11 line21 line31 line41 line51 line61 line71 line81 line91 
line2 line12 line22 line32 line42 line52 line62 line72 line82 line92 
line3 line13 line23 line33 line43 line53 line63 line73 line83 line93 
line4 line14 line24 line34 line44 line54 line64 line74 line84 line94 
line5 line15 line25 line35 line45 line55 line65 line75 line85 line95 
line6 line16 line26 line36 line46 line56 line66 line76 line86 line96 
line7 line17 line27 line37 line47 line57 line67 line77 line87 line97 
line8 line18 line28 line38 line48 line58 line68 line78 line88 line98 
line9 line19 line29 line39 line49 line59 line69 line79 line89 line99 
line10 line20 line30 line40 line50 line60 line70 line80 line90 line100
1
On

here is another one

$ printf "%s\n" line{1..100} | pr -10t

line1  line11 line21 line31 line41 line51 line61 line71 line81 line91
line2  line12 line22 line32 line42 line52 line62 line72 line82 line92
line3  line13 line23 line33 line43 line53 line63 line73 line83 line93
line4  line14 line24 line34 line44 line54 line64 line74 line84 line94
line5  line15 line25 line35 line45 line55 line65 line75 line85 line95
line6  line16 line26 line36 line46 line56 line66 line76 line86 line96
line7  line17 line27 line37 line47 line57 line67 line77 line87 line97
line8  line18 line28 line38 line48 line58 line68 line78 line88 line98
line9  line19 line29 line39 line49 line59 line69 line79 line89 line99
line10 line20 line30 line40 line50 line60 line70 line80 line90 line100
0
On
pr -ts" " --columns 10 file_name

Where file_name is the name of the file that you wish to convert.