Unix Command (Mac OS): cut and move rows Unix Command (Mac OS): cut and move rows shell shell

Unix Command (Mac OS): cut and move rows


Using a perl one-liner

perl -lne 'push @{$l[($.-1) % 3]}, $_; }{ print "@$_" for @l' data.txt | column -t

Explanation:

Switches:

  • -l: Enable line ending processing, specifies line terminator
  • -n: Creates a while(<>){..} loop for each line in your input file.
  • -e: Tells perl to execute the code on command line.

Code:

  • push @{$l[($.-1) % 3]}, $_;: Push each line into an array modulo the line number
  • }{ print "@$_" for @l: Print the 3 element array at end of processing
  • | column -t: Even out the columns


I would go with split and paste from coreutils. Try the following commands:

split -l3 infilepaste -d' ' xaa xab xac | column -t

Output:

1  a  i    4  d  iv  7  g  vii2  b  ii   5  e  v   8  h  viii3  c  iii  6  f  vi  9  i  xi


Here is a oneliner:

perl -ne 'chomp; push @a,$_ if $_; unless($. % 3) {push @f,[@a]; @a = undef; shift @a} END {for my $i (@f) { for (@$i) {print "$_ "} print "\n"}}' filename.txt

output

1 a i 2 b ii 3 c iii4 d iv 5 e v 6 f vi7 g vii 8 h viii 9 i xi