$run_env = "test";
$parm_file = "wf_PBM_DHT_CS90_CH_Hist_Extract.parm";
$sys_id = "PWLP";

#div name based on the session's call
$div = "WLD";

$CH_TYPE = "CS90CH";

#process name will be the process name in the trigger file

#sys_name would map to the applicable Tier e.g. FAC=Facets, WGS=WGS, STR=STAR…. 
$sys_name = "CS9";

# Target file directory
$TGT_DIR = "/u98_data/pcenterdata/"."$run_env"."/TgtFiles";

# Source file directory
$SRC_DIR = "/u98_data/pcenterdata/"."$run_env"."/SrcFiles";

# Parameter file directory
$prm_dir = "/u98_data/pcenterdata/"."$run_env"."/InfaParm";

# Target file
$OutputFile_CH_Filename=`grep "$OutputFile_CH_Type" $prm_dir/$parm_file | cut -d '=' -f 2`;

$psv_file=`grep "OUTPUT_EXCEL_PSV_FILE=" $prm_dir/$parm_file | cut -d '=' -f 2`;


# Here a zero byte file will be generated for division having zero record count 
# and that filename is wriiten into a file, for FTPing.

use Fcntl qw(:flock :seek);

if ( -s "$TGT_DIR/$OutputFile_CH_Filename" > 0) 
	print "Hello I am here\n";
	$target_file = `cat "$TGT_DIR/$OutputFile_CH_Filename"`;
	print "Hello completed\n";

	$start_date=`awk -F \'\\|\\\' \'\$1=="$PROCESS_NAME" && \$3=="$div" {print substr(\$9,3)}\' $SRC_DIR/$psv_file | head -1`;
	$end_date=`awk -F \'\\|\\\' \'\$1=="$PROCESS_NAME" && \$3=="$div" {print substr(\$10,3)}\'  $SRC_DIR/$psv_file | head -1`;


	print "Start Date : $start_date\n";
	print "End Date : $end_date\n";

	print "Zero Byte File For Div - $div is : $file_name \n";

	# Creating a zero byte file for division having zero record count
	`touch $TGT_DIR/$file_name`;

	# Write filename for the Division have zero records
	open(DAT,">$TGT_DIR/$OutputFile_CH_Filename") || die("Cannot Open File for writing filename for division having zero record count");
	flock(DAT, LOCK_EX);
	seek(DAT, 0, SEEK_SET); 
	print DAT "$file_name\n"; 

	$target_file = `cat "$TGT_DIR/$OutputFile_CH_Filename"`;


#print "Target File is : $target_file \n";

#Declarations for default values
$seg_identifier = "00";
$seg_identifier_tr = "99";
$fil = " ";
$filler_hdr_tlr = sprintf "%3220s", ("$$fil");
$source_sys_id = sprintf ("%-10s",$sys_id);

# ** Calculate the time stamp **
$dday = sprintf("%02d%02d", $hour,$min);
$ddate = sprintf("%4d%02d%02d", $year+1900,$mon+1,$mday);

# File type based on the run environment
if ("$run_env" eq "test" or "$run_env" eq "uat" ) 
	$file_type = "T";
	$file_type = "P";

# Header Record
$header = "$seg_identifier"."$source_sys_id"."$ddate"."$dday"."$file_type"."$div"."$filler_hdr_tlr";
#counting the no of records
$rec_cnt = `wc -l "$TGT_DIR/$target_file"`;
$rec_cnt = $1 if($rec_cnt=~/(\d+).*/);
$rec_cnt_align=sprintf("%010d", $rec_cnt);
print "rec_cnt: $rec_cnt_align \n";

# Trailer Record
$trailer = "$seg_identifier_tr"."$rec_cnt_align"."$filler_hdr_tlr";

$Header_File = $TGT_DIR."/"."CH_DHT_Rpt_Header";
$Trailer_File = $TGT_DIR."/"."CH_DHT_Rpt_Trailer";
$Detail_File = $TGT_DIR."/"."$target_file";
$CH_Report = $TGT_DIR."/"."$OutputFile_CH_Filename";

#----------------- Create the Header Record ---------------------------------#
print 'Head:'.$Header_File."Tail:".$Trailer_File;
# Write the Header record in the header file.
open HEADER_FILE, ">$Header_File" or die "Cannot open Header file";
print HEADER_FILE "$header";

#----------------- Create the Trailer Record ---------------------------------#

# Write the Trailer record in the Trailer file.
open TRAILER_FILE, ">$Trailer_File" or die "Cannot open Header file";
print TRAILER_FILE "$trailer";
# Cretet the report by merging the Header, Detail and Trailer Records

`cat $Header_File $Detail_File $Trailer_File > $CH_Report`;
`rm $Detail_File`;
`cp $CH_Report $Detail_File`;
`rm $CH_Report`;
`rm $Header_File`;
`rm $Trailer_File`;


I ahve been trying to add header and trailer to a huge file of size 1.5GB.
But after adding header it is showing out of memory in unix.
Could you please help me on this,it will be greatful!!

I ahve mentioned the code the cat command is throwing error I tried in many ways.


Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

#`cat $Header_File $Detail_File $Trailer_File > $CH_Report`;
#Instead of the above `cat ... etc. do the following
open OUT, '>', $CH_Report or die "Cannot open $CH_Report $!";

open IN, '<', $Header_File or die "Cannot open $Header_File $!";

while (<IN>) {
   print OUT $_;
print OUT "\n"; #Newline between header and detail
close IN;

open IN, '<', $Detail_File or die "Cannot open $Detail_File $!";

while (<IN>) {
   print OUT $_;
print OUT "\n"; #Newline between detail and trailer
close IN;

open IN, '<', $Trailer_File or die "Cannot open $Trailer_File $!";

while (<IN>) {
   print OUT $_;
close IN;
close OUT;
This article has been dead for over six months. Start a new discussion instead.