Friday 24 October 2014

Script to remove the Header and Trailer from the file




#!/usr/bin/ksh
set -x
# ******************************************************************************
# Description   : This script is designed to remove Header and Trailer record from
#                 any given file.
#                 The file has to be present in "$prmDirApeLanding" directory.
#
#                 This script uses parameters from a given 'unified' parameter file
#                 (ie. all parameters for running the job in datastage from the file)
#                 $1 - Parameter file path
#                 $2 - Unified Parameter file name
#                 $3 - File name of which Header and Trailer record needs to be removed.Filename should be without extension.
#                       Default extension ".dat" is used
#
###################################################################

print_formatted_date () {

  local print_file=$*
  set `date -u +"%a %d %h %Y %T"`                  09:17:46

  local dayw=$1
  local dayd=$2
  local month=$3
  local year=$4
  local time=$5
  local pid=$$
  printf "%-3s %2s %-3s %-4s %-8s GMT %-6s " ${dayw} ${dayd} ${month} ${year} ${time}  "${pid}:" >> $print_file

}
##############################################################################################
# Name: write_log
# Desc: write message to log prefixed by current date and time
# Args: $1 -> The message to be output
##############################################################################################

function write_log {

  print_formatted_date $script_log
  print $* >> $script_log

} # write_log

##############################################################################################
# Name: exit_script
# Desc: write relevant message to log  - exit script with passed exit code
# Args: exit code
##############################################################################################

function exit_script {

        if [[ $1 -eq 0 ]]
        then
                out_msg="Script remove_header_trailer.ksh - ended successfully"
        else
                out_msg="Script remove_header_trailer.ksh - ended abnormally"
        fi

        write_log "$out_msg"

        exit $1

} # exit_script

############################     MAIN    ##################################################

# **************************
#      SCRIPT STARTED
# **************************

#set -x
cd $1

cd  /data/ds/dpr_Project_dev/landing

remove_header_trailer_LNDG_TYCO_CUST_CONVT. param_3

cat $2 | tr -ds "$" "" > remove_header_trailer_$3.param_3

export `cat remove_header_trailer_$3.param_3`

chmod  777 remove_header_trailer_$3.param_3

cd $prmDirLanding

A=
script_log=$prmDirJobLog/remove_header_trailer_$3.log

rm -f remove_header_trailer_$3.log

write_log "Script remove_header_trailer.ksh $3 Started..."

infile=$prmDirLanding/$3
A=/data/ds/dpr_Project_dev/landing/lndg_tyco_cust_convtn

############  Match Input File Count with Trailer Count  ############
filecount=`wc -l ${infile}.dat |awk -F" " '{print $1}'`
filecount=103
hcount=` $filecount - 1`=102
tcount=` $hcount - 1`=101
filecount=$tcount
a=101
filecount=`echo $filecount | awk '{printf("%09d",$1)}'`
000000101

tailcount=`tail -1 ${infile}.dat | cut -c 11-20`
echo 'hcount = ' $hcount
echo 'tcount = ' $tcount
echo 'filecount = ' $filecount
echo 'tailcount=' $tailcount

mv ${infile}.dat ${infile}_orig.dat
    head -1 ${infile}_orig.dat > ${infile}_header.dat
    chmod 766 ${infile}_orig.dat
    head -${hcount} ${infile}_orig.dat | tail -${tcount} > ${infile}.dat
    chmod 766 ${infile}.dat
    write_log "Header and Trailer records are removed from $3 file "

App_ID=`head -1 ${infile}_orig.dat | cut -c11-13`
ID_In_Ref_File_Ind=`grep "${App_ID}" $prmDirParam/App_Id_ref.dat | wc -l | tr -d [:space:]`


if [ ${ID_In_Ref_File_Ind} -eq 1 ]
then
DataValidated=`head -1 ${infile}_orig.dat | cut -c116`
    if [ ${DataValidated} = 'F' ]
    then
    write_log "File Level Audit Failed"
    write_log "File Header contains "F" as Data Validated"
    write_log "Script remove_header_trailer.ksh $3 - ended abnormally"
    exit 5
    fi
fi

if [ $filecount -eq $tailcount ]
then
    write_log "File Count and Trailer Count is matching "
    write_log "File Count: $filecount Trailer Count: $tailcount"
 
    cd $1
    files=`ls remove_header_trailer_$3.param_3`
    write_log "Deleting temporary file: $files."
    rm -f $files

    exit 0

else
    write_log "File Count and Trailer Count is NOT matching "
    write_log "File Count: $filecount  Trailer Count:  $tailcount"
    write_log "### Invalid $3 File Received ### "
    write_log "Script remove_header_trailer.ksh $3 - ended abnormally"

    cd $1
    files=`ls remove_header_trailer_$3.param_3`
    write_log "Deleting temporary file: $files."
    rm -f $files

    exit 5
fi

No comments:

Post a Comment