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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| #!/usr/bin/perl
#############################################################################
#FONCTIONS
## Restrait des espaces dans une chaine de caractères
sub del_space
{
$lig = $_[0];
$new_lig = "";
$taille_lig = length($lig);
$i = 0;
while ($i <= $taille_lig-1)
{
if (substr($lig,$i,1) ne " ")
{
$new_lig = $new_lig.substr($lig,$i,1);
}
$i++;
}
$lig = $new_lig;
}
## Extraction des valeurs des paramètres : ND-NE-NDG-NDS-TAX-TY-CAT-MAR
sub nd_val
{
$nd = $_[0];
$nd = substr($nd,3,8);
$nd = $nd;
}
sub ne_val
{
$ne = $_[0];
$ne = substr($ne,3,10);
$ne = $ne;
}
sub tax_val
{
$tax = $_[0];
$tax = substr($tax,4,8);
$tax = $tax;
}
sub ndg_val
{
$ndg = $_[0];
$ndg = substr($ndg,4,8);
$ndg = $ndg;
}
sub nds_val
{
$nds = $_[0];
$nds = substr($nds,4,8);
$nds = $nds;
}
sub ty_val
{
$ty = $_[0];
$taille_ty = length($ty);
$ty = substr($ty,3,$taille_ty-3);
$ty = $ty;
}
sub cat_val
{
$cat = $_[0];
$taille_cat = length($cat);
$cat = substr($cat,4,$taille_cat-4);
$cat = $cat;
}
sub mar_val
{
$mar = $_[0];
$taille_mar = length($mar);
$mar = substr($mar,4,$taille_mar-4);
$mar = $mar;
}
#############################################################################
#ouverture du fichier original
open(fich, $ARGV[1]);
#creation du fichier sans les NDS et les NDG
$intfilename = $ARGV[1].'_1';
open(F, '>'.$ARGV[1].'_1');
while ($ligne = <fich>)
{
chomp($ligne);
if ((index($ligne,"NDS") < 0) && (index($ligne,"NDG") < 0))
{
printf (F "%s \n", &del_space($ligne));
}
}
close F;
close fich;
#ouverture du fichier sans les NDS et les NDG
open(fich, $ARGV[1].'_1');
#creation du nouveau fichier : fichier plat avec separateur ;
$filename = $ARGV[1].'_2.TXT';
open(F, '>'.$ARGV[1].'_2.TXT');
$ND = "";
$ND_ANC="";
$NE="";
$NDG="";
$NDS="";
$TAX="";
$TY="";
$CAT="";
$MAR="";
$nb_nd = 0;
while ($ligne = <fich>)
{
chomp($ligne);
if (index($ligne,"ND=") >= 0)
{
$nb_nd++;
$ND_ANC = $ND;
$ND = substr($ligne,index($ligne,"ND="),11);
if (($nb_nd > 1) && ($ND_ANC ne ""))
{
$new_line = &nd_val($ND_ANC).";".&ne_val($NE).";".$NDG.";".$NDS.";".&tax_val($TAX).";".&ty_val($TY).";".&cat_val($CAT).";".&mar_val($MAR);
printf (F "%s \n", $new_line);
$NE="";
$TAX="";
$TY="";
$CAT="";
$MAR="";
}
if (index($ligne,"NE=") >= 0)
{
$NE = substr($ligne,index($ligne,"NE="),13)
}
if (index($ligne,"TAX=") >= 0)
{
$TAX = substr($ligne,index($ligne,"TAX="),12)
}
}
else
{
if (index($ligne,"TY=") >= 0)
{
$TY = substr($ligne,index($ligne,"TY="),length($ligne)-1)
}
if (index($ligne,"CAT=") >= 0)
{
$CAT = substr($ligne,index($ligne,"CAT="),length($ligne)-1)
}
if (index($ligne,"MAR=") >= 0)
{
$MAR = substr($ligne,index($ligne,"MAR="),length($ligne)-1)
}
}
}
$new_line = &nd_val($ND).";".&ne_val($NE).";".$NDG.";".$NDS.";".&tax_val($TAX).";".&ty_val($TY).";".&cat_val($CAT).";".&mar_val($MAR);
printf (F "%s \n", $new_line);
close F;
close fich;
$newfilename = $ARGV[0].'_'.$ARGV[1].'_'.$ARGV[2].'_'.$ARGV[3].'.TXT';
rename($filename, $newfilename);
unlink($intfilename); |
Partager