0

I have three files in a folder.

File A have 700 lines, made up of 100 data blocks. Each datablock have 5 lines. First line has the total number of lines per datablock, 2nd line is empty, and 3~7th line are data.

5  

AA  356djs  225gsd  1245gr
BB  123asd  123asd  123asd
CC  TToptg  TToptg  gngngn
DD  sIG123  gjn123  uot123
EE  166131  100021  205011
5 

AA  356djs  225gsd  1245gr
BB  123asd  123asd  123asd
CC  TToptg  TToptg  gngngn
DD  sIG123  gjn123  uot123
EE  166131  100021  205011
.... (repeating until 100th data block)  

File B is similar with A, but a bit different. It has 300 total lines, made up of 100 datablocks, each datablock has 3 lines

3

LL  lplplp  122121  aggagg
KK  hbnkio  ohgimp  125125
TT  KGNskg  fgnjdg  125154
3

LL  lplplp  122121  aggagg
KK  hbnkio  ohgimp  125125
TT  KGNskg  fgnjdg  125154
.... (repeating until 100th data block)

File C is also similar. 400 lines total, made up of 100 datablocks, each datablock has 4 lines

4

PP  ginini  216361  sgdaga
ZZ  gonhon  q215ag  hagqgq
RR  TKEMMM  125sdg  125961
II  tninks  150121  192u9u
4

PP  ginini  216361  sgdaga
ZZ  gonhon  q215ag  hagqgq
RR  TKEMMM  125sdg  125961
II  tninks  150121  192u9u
.... (repeating until 100th data block)

I hope to stack up those 3 files to single file, datablock by datablock. So the results will have total 1200 lines, made up of 100 datablocks, each datablock has 12 lines, and should be look like

12

AA  356djs  225gsd  1245gr
BB  123asd  123asd  123asd
CC  TToptg  TToptg  gngngn
DD  sIG123  gjn123  uot123
EE  166131  100021  205011
LL  lplplp  122121  aggagg
KK  hbnkio  ohgimp  125125
TT  KGNskg  fgnjdg  125154
PP  ginini  216361  sgdaga
ZZ  gonhon  q215ag  hagqgq
RR  TKEMMM  125sdg  125961
II  tninks  150121  192u9u
12

AA  356djs  225gsd  1245gr
BB  123asd  123asd  123asd
CC  TToptg  TToptg  gngngn
DD  sIG123  gjn123  uot123
EE  166131  100021  205011
LL  lplplp  122121  aggagg
KK  hbnkio  ohgimp  125125
TT  KGNskg  fgnjdg  125154
PP  ginini  216361  sgdaga
ZZ  gonhon  q215ag  hagqgq
RR  TKEMMM  125sdg  125961
II  tninks  150121  192u9u
.... (repeating until 100th data block)

If it were stacking up of 3 files at all, it is very easy, since I can use cat command. But this is different.... How can I do this stacking up of files, by each data blocks, like the example above? Can I use awk command or cat command? Fortran or python methods are also welcome.

Thanks

Best,

exsonic01
  • 615
  • 7
  • 27

1 Answers1

1

Here is Perl code for stacking the blocks. It temporarily redefines $/ (record separator) as two consecutive new lines so each blank line starts a new paragraph. It then reads each paragraph (block) from each of the three files and writes to output.

#!/usr/bin/env perl

# Usage: $0 <File A> <File B> <File C> <Output File>

StackEm();
CleanUp();

sub StackEm
{
  # Within this function, temporarily redefine $/ for paragraph mode
  local $/ = "\n\n";
  my $line;
  open(A, "< $ARGV[0]");
  open(B, "< $ARGV[1]");
  open(C, "< $ARGV[2]");
  open(D1, "> temp1");

  # Read a block from each of the files and write it to output
  for (my $i=0; $i < 100; $i++) {
    $line = <A>;
    print D1 "12\n";
    print D1 $line;

    $line = <B>;
    print D1 $line;

    $line = <C>;

    print D1 $line;
  }

  close(A);
  close(B);
  close(C);
  close(D1);

}

sub CleanUp
{
  open(D2, "< temp1");
  open(E, "> $ARGV[3]");

  while (<D2>) {
    if ( /^\s*(\d+)\s*$/ ) {
      if ( $1 == 12  && ($. != 1) ) {
        print E $_, "\n";
      }
    } elsif ( !($_ =~ /^\s*$/) )  {
        print E $_;
    }
  }

  close(D2);
  close(E);
}
ramana_k
  • 1,933
  • 2
  • 10
  • 14
  • I have only limited idea about perl, but I got a hint from this code. I finished coding with the fortran 90 THanks – exsonic01 Jul 22 '15 at 16:21