#!/usr/bin/perl


$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
$PROCESS_NAME = "CH_CS90";

#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_Type='OutputFile_'."$CH_TYPE"."_Filename=";
$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`;

chomp($OutputFile_CH_Filename);
chomp($psv_file);

# 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";
}
else

{	
	$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`;

	chomp($start_date);
	chomp($end_date);

	print "Start Date : $start_date\n";
	print "End Date : $end_date\n";
	
	$file_name="CLM_"."$div"."_"."$sys_name"."_PBM_S"."$start_date"."_E"."$end_date".".txt";

	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"; 
	close(DAT);

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

chomp($target_file);

#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 **
($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);
$year+1900,$mon+1,$mday,$hour,$min,$sec;
$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";
	}
else
	{
	$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";
chomp($Detail_File);


#----------------- 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";
close HEADER_FILE;

#----------------- 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";
close TRAILER_FILE;
# 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`;

Hi,

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.

Sanjana

#`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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.