Hi,
Basically I work on EDI file.
The script will convert each record(row) in the CSV file to sterling format.
The number of records keeps varying.
The scripts goes here:
#!/bin/ksh
export LIBPATH=/ediprod6.1/gentran/lib:/usr/local/lib
cd /ediprod6.1/gentran/comm_EUROCSV/
infiles=`ls *csv 2>/dev/null` ## List all input files
if test -n $infiles 2>/dev/null
then
ls *csv|while read i ## Read each input files
do
echo "FILE NAME:" >notify.$i.mail
echo "$i" >>notify.$i.mail ## ls in.$i >>notify.$i.mail
###echo "FILE DATA:" >>notify.$i.mail
###cat in.$i >>notify.$i.mail
########### FILE EDITING PROCESS ######################################
sed s/
//g <$i >$i.tmp
mv $i.tmp $i
## Declare unique variable as DateTime(dttm)
dttm=`date +%y%m%d%H%M%S`
########### END OF FILE EDITING PROCESS ###############################
############## FILE TRANSLATION PROCESS ###############################
ERROR=0
rm /ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err
rm /ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err
touch /ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err
touch /ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err
## Translate file
lftran $i "-ofT[TMN91_SPICER]" transport.$i.out.$dttm -cp "/ediprod6.1/gentran"
res=$?
if [ $res -ne 0 ] ## Check if there is ERROR
then
##### Generate ERROR DETAILS USING xlcntl.err and dtlLog.err ####
C=0
File_xln=/ediprod6.1/gentran/comm_EUROCSV/temp/xlcntl.err
File_dtl=/ediprod6.1/gentran/comm_EUROCSV/temp/dtlLog.err
echo "########## ERROR DETAILS FOR $i ##########" >ErrorDetail_of_$i.txt ## Generate Error details txt File
grep "in.*.app" $File_xln >app_lin.tmp
awk 'BEGIN {FS = "/"} {print $NF} ' app_lin.tmp >app_name.tmp
grep "Record count" $File_xln >rec_cnt.tmp
awk 'BEGIN {FS = ":"} {print $NF } ' rec_cnt.tmp >rec_name.tmp
grep "Translation error count" $File_xln >err_cnt.tmp
awk 'BEGIN {FS = ":"} {print $NF} ' err_cnt.tmp >err_name.tmp
C=`cat app_name.tmp|wc -l`
###C=`expr $C + 1`
Line=1
while [ $Line -le $C ];
do
err_num=0
app_name=`tail +$Line app_name.tmp|head -1`
rec_name=`tail +$Line rec_name.tmp|head -1`
err_num=`tail +$Line err_name.tmp|head -1`
echo "Record Number :$rec_name" >>ErrorDetail_of_$i.txt
grep -n $app_name $File_dtl >file_name.tmp
line_number=0
line_number=`awk 'BEGIN {FS = ":"} {print $1 } ' file_name.tmp`
err_num=`expr $err_num + 1`
L=1
while [ $L -lt $err_num ];
do
####Get Decription
echo "Error Number : $L" >>ErrorDetail_of_$i.txt
if [ $L -eq 1 ]; then
line_number=`expr $line_number + 23`
fi
if [ $L -gt 1 ]; then
line_number=`expr $line_number + 5`
fi
tail +$line_number $File_dtl|head -1 > err_desc.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' err_desc.tmp >err_desc1.tmp
err_desc=`awk 'BEGIN {FS = "<"} {print $1 } ' err_desc1.tmp`
echo "Error Description : $err_desc" >>ErrorDetail_of_$i.txt
####Get Element Name
line_number=`expr $line_number + 19`
tail +$line_number $File_dtl|head -1 > elm_name.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' elm_name.tmp >elm_name1.tmp
elm_name=`awk 'BEGIN {FS = "<"} {print $1 } ' elm_name1.tmp`
echo "Element Name : $elm_name" >>ErrorDetail_of_$i.txt
####Get Value
line_number=`expr $line_number + 4`
tail +$line_number $File_dtl|head -1 > elm_value.tmp
awk 'BEGIN {FS = ">"} {print $2 } ' elm_value.tmp >elm_value1.tmp
elm_value=`awk 'BEGIN {FS = "<"} {print $1 } ' elm_value1.tmp`
echo "Element Value : $elm_value" >>ErrorDetail_of_$i.txt
L=`expr $L + 1`
done
echo "---" >>ErrorDetail_of_$i.txt
Line=`expr $Line + 1`
done
rm *.tmp 2>/dev/null
########## END of Generate ERROR#########
## Send ERROR notification via email
uuencode ErrorDetail_of_$i.txt ErrorDetail_of_$i.txt |mailx -s "PROBLEM TRANSLATING TMN91 SPICER FILE named $i. FILE MOVED TO ERROR"
xyz@abc.com
## mv in.$i.$dttm /ediprod6.1/gentran/error
ERROR=1
rm $i 2>/dev/null
rm transport.$i.out.$dttm 2>/dev/null
rm notify.$i.mail 2>/dev/null
fi
if [ $ERROR -ne 1 ] ## If there is no ERROR
then
echo " " >>notify.$i.mail
echo "TRL COUNT AFTER TRANSLATION PROCESS:" >>notify.$i.mail
grep ^TRL transport.$i.out.$dttm | wc -l >>notify.$i.mail
## Send Notification email
cat notify.$i.mail |mail -s "TMN91 Spicer FILES ${dttm}"
xyz@abc.com
## Script to remove multiple HDR TRL (HT)
## trl=0
## inFile=transport.$i.out.$dttm
## resultFile=resultFile.tmp
## head -1 $inFile >>$resultFile
## trl=`grep ^TRL $inFile |wc -l`
## sed -e /^HDR/d $inFile >> tempFile1
## sed -e /^TRL/d tempFile1 >> $resultFile
## rm tempFile1
## tail=$trl
## echo "TRL|"`expr $trl`>>$resultFile
## Script Separate
FILE=transport.$i.out.$dttm
C=`cat $FILE|wc -l`
C=`expr $C + 1`
Line=1
F_Count=1
while [ $Line -lt $C ];
do
touch $F_Count.tmp
temp_line=`tail +$Line $FILE|head -1`
start=`echo $temp_line|cut -c 1-3`
if [ $start != "TRL" ]; then
echo $temp_line>>$F_Count.tmp
else
echo $temp_line>>$F_Count.tmp
F_Count=`expr $F_Count + 1`
fi
Line=`expr $Line + 1`
done
## INV Manipulator
File_C=1
while [ $File_C -lt $F_Count ];
do
INV=`grep ^INV $File_C.tmp` # INV = INV line of current file
if [ "$INV" != "" ]; then
INV1=`cat $File_C.tmp|grep ^INV|cut -d "|" -f2`
INV3=`cat $File_C.tmp|grep ^INV|cut -d "|" -f4`
INV_O=`echo $INV3` ## INV3 original
cat $File_C.tmp|grep ^SHP|cut -d "|" -f16 > SHP_F15.tmp
F=`expr $File_C + 1`
while [ $F -lt $F_Count ] ;
do
INV_F1=`cat $F.tmp|grep ^INV|cut -d "|" -f2`
if [ "$INV_F1" = "$INV1" ];then
SHP_F15=`cat $F.tmp|grep ^SHP|cut -d "|" -f16`
echo $SHP_F15>>SHP_F15.tmp
## Delete INV line from $F.tmp ##
touch temp.tmp
grep -v "^INV" $F.tmp > temp.tmp
cat temp.tmp>$F.tmp
rm temp.tmp
fi
F=`expr $F + 1`
done
## Replace INV3 ##
awk 'BEGIN{AMT=0}{
AT=$1
AMT=AMT + AT;}
END{printf("%.2f",AMT)}' SHP_F15.tmp> INV3.tmp
INV3=`cat INV3.tmp`
echo $INV>INV.tmp
sed s/"|$INV_O|"/"|$INV3|"/ INV.tmp>temp.tmp
cat temp.tmp>INV.tmp
rm temp.tmp
touch temp.tmp
INV_New=`cat INV.tmp`
sed s/"$INV"/"$INV_New"/ $File_C.tmp > temp.tmp
cat temp.tmp>$File_C.tmp
rm temp.tmp
touch temp.tmp
fi
File_C=`expr $File_C + 1`
done
## Script Merge
File_C=1
rm temp.tmp 2>/dev/null
touch temp.tmp
while [ $File_C -lt $F_Count ];
do
cat $File_C.tmp>>temp.tmp 2>/dev/null
File_C=`expr $File_C + 1`
done
cat temp.tmp>FILE_merge.tmp
rm temp.tmp 2>/dev/null
### removing Multiple HRD TRL
rm resultFile.tmp tempFile.tmp 2>/dev/null
touch resultFile.tmp tempFile.tmp
head -1 FILE_merge.tmp >>resultFile.tmp
tail=`grep ^HDR FILE_merge.tmp |wc -l`
sed -e /^HDR/d FILE_merge.tmp >> tempFile.tmp
sed -e /^TRL/d tempFile.tmp >> resultFile.tmp
echo "TRL|"`expr $tail`>>resultFile.tmp
#//
cat resultFile.tmp>transport_transport_110.SPICER.$dttm ## Create Output file
rm $resultFile
rm *.tmp 2>/dev/null
########## END OF TRANSLATION PROCESS ##################
## Move Translated output file to FTP
###mv transport_transport_110.100998.* /ediprod6.1/gentran/ftp_trans_ran 2>/dev/null
mv transport_transport_110.SPICER.* /ediprod6.1/gentran/comm_EUROCSV/test 2>/dev/null
## Update in Legal( infile and notification ) directory
cat $i >/ediprod6.1/gentran/legal/FEDXSMART/TMN91.in.$dttm 2>/dev/null
cat notify.$i.mail >/ediprod6.1/gentran/legal/FEDXSMART/TMN91.email.$dttm 2>/dev/null
## move infile in "Legal/CLIENT" directory
mv $i /ediprod6.1/gentran/legal/EURO/TMN91/in.dane1.$dttm 2>/dev/null
fi
done
## Remove all temp files
rm notify* 2>/dev/null
rm in* 2>/dev/null
rm *out 2>/dev/null
rm out* 2>/dev/null
rm transport* 2>/dev/null
rm *.csv 2>/dev/null
else
echo "No TMN91 Spicer files today" |mail -s "TMN91 Spicer ${dttm}"
xyz@abc.com
fi
Thanks for your time.