How to shift values in new column after certain interval?

Hello everyone,

my data is in a format as below

a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a

how can i shift the data to new column after every 4 values so that i should like like this


a|a|a|a|
a|a|a|a|
a|a|a|a|
a|a|a|a| 

:smiley:

Hi

# awk 'NR%4{printf $0 "|"; next;}{print $0 "|";}END{print ""}'  a
a|a|a|a|
a|a|a|
#

Guru.

Hi jojo

Would this be what you are looking for?

#! /usr/bin/perl

use strict;
use warnings;

my $count = 0;
my $matrix;
my $x = 0;
my $y = -1;


open(my $X,"inputfile.txt") or die "error opening the inputfile\n";
while(<$X>)
{
   chomp;
   if( $count%4 == 0 ){
      $x = 0;
      $y++;
   }
      $matrix->[$x++]->[$y] = $_;
      $count++;
}
close $X;

for($x = 0; $x < 4; $x++)
{
   for($y = 0; $y < 4; $y++)
   {
   if (exists $matrix->[$x]->[$y])
   {
         print $matrix->[$x]->[$y];
   }
   }
   print "\n";
}

---------- Post updated at 11:36 AM ---------- Previous update was at 11:29 AM ----------

Guru,

Can you explain what it is you have done? I know the basics of awk. So ive like half understood your script.

Thanks

well thnkz a lot for the reply,

but i think i m not getting the expected result.

well its like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

now i want the output as

1|6|11
2|7|12
3|8|13
4|9|14
5|10|15

Hi, Something like this,

paste -d '|' - - - - <  inputfile

appreciate your reply

but as per your comment the output is as follows

1|2|3|4
5|6|7|8
9|10|11|12

supposing you don't have any file bginning with 'x' in the curdir

split -l $(($(wc -l <file)/4+1)) file
paste -d'|' x*

but when i try to run the first command its states

Variable syntax.

An approach with awk:

awk '{a[NR%5]=a[NR%5]?a[NR%5]"|"$0:$0}
END{for(i=1;i<5;i++)print a;print a[0]}
' file