I have two files
fileA
HEADER LINE A
CommentLine A
Content A
....
....
....
TAILER A
fileB
HEADER LINE B
CommentLine B
Content B
....
....
....
TAILER B
I want to merge these two files as
HEADER LINE A
CommentLine A
Content A
....
....
....
Content B
....
....
....
TAILER B
i.e. skip the TAILER line of file A and skip the HEADER and Comment Line of fileB
I am able to do it using the below perl code
open ( FA, "$fileA" ) || die("can't open fileA $!");
open ( FB, "$fileB" ) || die("can't open fileB $!");
open ( TMP, ">> tmp_file" ) || die("can't open tmp_file $!");
#reading both files in array
my @fileA = <FA>;
my @fileB = <FB>;
#getting rid of HEADER, Comment line, in fileB
shift @fileB;
shift @fileB;
#getting rid of TAILER in fileA
pop @fileA;
my @tmp_file=(@fileA,@fileB);
foreach ( @tmp_file ){
print TMP $_;
}
close(FA);
close(FB);
close(TMP);
rename tmp_file, fileA || die("can't rename tmp_file to fileA);
This code works fine, however I doubt it's efficiency if fileA and fileB are going to be millions of lines (which is the case)
i.e. why read whole file in arrays just to get rid of three lines (will end up using lots of memory)
Can someone suggest a more efficient way of doing this
(answers in perl only)