j'ai récupéré un code en ksh j'aimerai l'optimiser avant de le convertir en perl

je vais vous publier les bouts de codes qui reviennent assez souvent

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
function FORMAT_PTF
{
	INPUT=$1
	OUTPUT=$2
	CONTROL=$3
	NBLINE=`wc -l ${INPUT} | awk '{print $1}'`
	#DEBUT CONTROLE ET FORMATAGE DU FICHIER ARPSON
	echo "DEBUT CONTROLE ET FORMATAGE DU FICHIER $INPUT"	
	cat "$INPUT | awk '"
		#
		#Initialisation des variables
		BEGIN	{      																
			#initialisation du separateur des champs
			FS="\t";				
			#initialisation de la 1er ligne du tableau bidimensionnel FIELDS avec le nom, longueur champs, 
			#Type de champ		 
			FIELDS[1,1] = "PTF_13_F";
			FIELDS[1,2] = "COMPANY_2_F"; 
			FIELDS[1,3] = "DESK_3_T";
			FIELDS[1,4] = "BOOK_3_T";
			FIELDS[1,5] = "SYSTEM_1_T";
			FIELDS[1,6] = "DESKARPSON_10_T";
			FIELDS[1,7] = "CODE_12_T";
			FIELDS[1,8] = "TYPE_1_T";
			FIELDS[1,9] = "CODEARPSONISIN_4_T";
 
			#initialisation de la 2eme ligne du tableau bidimensionnel FIELDS avec les libelles des champs
			FIELDS[2,1] = "Portefeuille";
			FIELDS[2,2] = "Departement du siege de la CEP";
			FIELDS[2,3] = "Affectation DRG";
			FIELDS[2,4] = "ACT";
			FIELDS[2,5] = "A pour Arpson";
			FIELDS[2,6] = "Affectation Arpson";
			FIELDS[2,7] = "Code Isin tronque ou num histo";
			FIELDS[2,8] = "Typologie produit";
			FIELDS[2,9] = "";
 
			#Definition des codes retours et les messages associes
			CodLengthErr[0] = "1001";		CodLengthErr[1] = "Longueur chaine de caracteres incorrecte";
			CodFormatDatErr[0] = "1002";	CodFormatDatErr[1] = "Format date incorrect";
			CodFormatIntErr[0] = "1003";	CodFormatIntErr[1] = "Format entier numerique incorrecte"; 
			CodFormatDblErr[0] = "1004";	CodFormatDblErr[1] = "Format numerique incorrect";	
			CodDataErr[0] = "1005";		CodDataErr[1] = "Donnee obligatoire";
			CodDefaultValErr[0] = "1006";	CodDefaultValErr[1] = "Valeur par defaut";
 
			#intialisation des champs du fichier log des anomalies 
			IdentFile="PTF.ast";
			IdentField="";			
			IdentWarn=0;
			IdentlineWarn="";
			CodWarn = "";
			FormatWarn = "";
			DataWarn = "";
 
			#initialisation des variables temporaires
			MESSAGE="";			
			NbLineWarn=0;
			PROBLEM="FALSE";
			ValTypCod=""; #Valeur selon ISIN/NumHist
 
			#initialisation des champs du fichier PTF.ast
			B_PTF1=""; #1er champs:Code du portefeuille
			B_PTF2=""; #2eme champs:Code du protefeuille
			B_GRP=""; #3eme champs:Groupe
 
			#Version 1.1: voir description
			#initialisation du tableau ReplaceChar contenant les caracteres a rempalcer par espace
			ReplaceChar[1,1]="-";	ReplaceChar[1,2]=" ";
			ReplaceChar[2,1]="_";	ReplaceChar[2,2]=" ";
			ReplaceChar[3,1]="\\";	ReplaceChar[3,2]=" ";
			ReplaceChar[4,1]="/";	ReplaceChar[4,2]=" ";
			LenRepChar=4;
      		}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
      		function GetWarning()
        	{ 
        		return sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s",IdentFile,IdentField,IdentWarn,IdentlineWarn,CodWarn,FormaWarn,DataWarn);         		
        	}
 
        	function LTrim(Field)
        	{
        		for(j=0;substr(Field,j,1) == " ";j++);
        		return substr(Field,j,length(Field)-j+1);     
        	}
 
        	#Version 1.1: voir description
        	function CharToSpace(Field,ReplaceChar,LenRepChar)
		{
			sField = Field;
			for (i=1; i<=LenRepChar; i++)
			{
				sChar = ReplaceChar[i,1];
				if ( sChar == "\\" )
				{
					gsub("\\"sChar, ReplaceChar[i,2], sField);	
				}
				else
				{							
					gsub(sChar, ReplaceChar[i,2], sField);
			}	}		
			return sField;						
		}
#Corps du programme : Controle de la structure du fichier
/[^$]/{
#Recuperation du code ISIN/NumHist
if ( substr($9,1,4) == "ISIN" )
{
ValTypCod=sprintf("%s%s",substr($7,1,1),substr($7,3,length($7)-3));
}
else
{
ValTypCod=sprintf("%s",substr($7,3,length($7)-2));
}
B_PTF1 = sprintf("%s%s%s%s%s%s%s",$2,$3,$4,$5,$6,ValTypCod,$8);
#Controle des champs
for (i=1; i<NF-1; i++)
{
split(FIELDS[1,i],SUBTAB,"_");
#Control anomalie 1001
if ( length($i) != SUBTAB[2])
{
IdentField = FIELDS[2,i];
IdentWarn++;
IdentlineWarn = B_PTF1;
CodWarn = CodLengthErr[0];
FormaWarn = CodLengthErr[1];
DataWarn = $i;
MESSAGE = sprintf("%s%s\n",MESSAGE,GetWarning());
PROBLEM="TRUE";
}
#Control anomalie 1005
if(LTrim($i) == "")
{
IdentField = FIELDS[2,i];
IdentWarn++;
IdentlineWarn = B_PTF1;
CodWarn = CodDataErr[0];
FormaWarn = CodDataErr[1];
DataWarn = $i;
MESSAGE = sprintf("%s%s\n",MESSAGE,GetWarning());
PROBLEM="TRUE";
}
}
#Incrementation des nombres de lignes en anomalies
if ( PROBLEM == "TRUE" )
{
NbLineWarn++;

}
#Construction d un enregistrement
B_PTF2 = B_PTF1;
B_GRP = sprintf("%s%s%s%s%s",$2,$3,$4,$5,$6);

#Version 1.1: voir description
printf("%s\t%s\t%s\t%s\t\t\n",$1,CharToSpace(B_PTF1,ReplaceChar,LenRepChar) \
,CharToSpace(B_PTF2,ReplaceChar,LenRepChar) \
,CharToSpace(B_GRP,ReplaceChar,LenRepChar));
TABNR[NR] = MESSAGE;
#Initialisation des variables
B_PTF1="";
B_PTF2="";
B_GRP="";
MESSAGE = "";
PROBLEM = "FALSE";

}
#
#Recuperation des lignes erronnees ainsi que le nombre des lignes erronnees
END {
for ( i=1 ;i<=NR ;i++)
{
printf("%s",TABNR[i]);
}

printf("%s\n",NbLineWarn);

}"'" >${OUTPUT}.tmp
CODERR=$?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
#RECUPERATION DU FICHIER DE CONTROLE				
	if [ $CODERR -ne 0 ]
	then		
		echo "[Erreur] : le fichier ${OUTPUT}.tmp n a pu etre cree"
		exit 1 
	else				
		#FIN CONTROLE ET FORMATAGE DU FICHIER ARPSON       
		echo "FIN CONTROLE ET FORMATAGE DU FICHIER $INPUT" 
 
		NBLINE_TOT=`wc -l ${OUTPUT}.tmp | awk '{print $1}'`
 
		#Recuperation du nombre de lignes reel traite sans la ligne consacre au nombre des lignes en erreur
		REALNBLINE_TOT=`echo ${NBLINE_TOT}-1|bc`
 
		if [ ${REALNBLINE_TOT} -ne ${NBLINE} ]
		then		
			echo "PROBLEME SUR LE FICHIER PTF GENERE"			
			#recuperation du nombre des lignes en anomalies
			NBLINE_ANO=`cat ${OUTPUT}.tmp | tail -1`
			echo "RECUPERATION DU FICHIER USER LOG"
			#il faut soustraire la ligne portant le nombre des lignes erronnees et les lignes des messages d erreurs
			NBLINE_LOG=`echo ${REALNBLINE_TOT}-${NBLINE}|bc` 			
			NBLINE_MES=`echo ${NBLINE_LOG}+1 |bc` 
			echo "Nombre de ligne en entree: "$NBLINE | tee -a $CONTROL						
			echo "Nombre de ligne avec anomalie: "$NBLINE_ANO | tee -a $CONTROL			
			echo "Nombre de ligne en sortie: "$NBLINE | tee -a $CONTROL			
			echo "Nombre d'anomalies: "$NBLINE_LOG | tee -a $CONTROL			
			echo "\nErreur sur les lignes suivantes:" | tee -a $CONTROL							
			cat ${OUTPUT}.tmp | tail -$NBLINE_MES | head -${NBLINE_LOG} | tee -a $CONTROL			
			cat ${OUTPUT}.tmp | head -${NBLINE} > ${OUTPUT}		
 
		else	
			cat ${OUTPUT}.tmp | head -${NBLINE} > ${OUTPUT}
			touch $CONTROL		
			echo "PAS DE PROBLEMES SUR LE FICHIER PTF GENERE" 
		fi
		rm -f ${OUTPUT}.tmp
	fi												
}
si y'en a qui ont des idées