• Publicidad

Recorrido de ficheros y directorio

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Recorrido de ficheros y directorio

Notapor JoshZeta » 2016-01-26 19:33 @856

Buenas con todos :D , otra vez por acá molestándolos :roll:

Tengo un código que me permite recorrer un archivo y a partir de éste generar otros archivos distintos; esto lo hago así porque el archivo original contiene registros por hora de todo un día pero necesito que éstos estén en archivos distintos.

El código es el siguiente:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2.  
  3. my $nombre_arch_fuen2 = "/home/scadmin/data_M2000/PDP/PDP_2016-01-21 00\:00_2016-01-22 00\:00.csv";# aca escribo manualmente el nombre del archivo que quiero dividir
  4.  
  5. my $nom1 = substr($nombre_arch_fuen2,29,3);
  6. my $nom2 = substr($nombre_arch_fuen2,32,12);
  7. my $nom3 = substr($nombre_arch_fuen2,49,12);
  8.  
  9. my $S1 = $nom1.$nom2."00\:00".$nom2."01\:00.csv";
  10. my $S2 = $nom1.$nom2."01\:00".$nom2."02\:00.csv";
  11. my $S3 = $nom1.$nom2."02\:00".$nom2."03\:00.csv";
  12. my $S4 = $nom1.$nom2."03\:00".$nom2."04\:00.csv";
  13. my $S5 = $nom1.$nom2."04\:00".$nom2."05\:00.csv";
  14. my $S6 = $nom1.$nom2."05\:00".$nom2."06\:00.csv";
  15. my $S7 = $nom1.$nom2."06\:00".$nom2."07\:00.csv";
  16. my $S8 = $nom1.$nom2."07\:00".$nom2."08\:00.csv";
  17. my $S9 = $nom1.$nom2."08\:00".$nom2."09\:00.csv";
  18. my $S10 = $nom1.$nom2."09\:00".$nom2."10\:00.csv";
  19. my $S11 = $nom1.$nom2."10\:00".$nom2."11\:00.csv";
  20. my $S12 = $nom1.$nom2."11\:00".$nom2."12\:00.csv";
  21. my $S13 = $nom1.$nom2."12\:00".$nom2."13\:00.csv";
  22. my $S14 = $nom1.$nom2."13\:00".$nom2."14\:00.csv";
  23. my $S15 = $nom1.$nom2."14\:00".$nom2."15\:00.csv";
  24. my $S16 = $nom1.$nom2."15\:00".$nom2."16\:00.csv";
  25. my $S17 = $nom1.$nom2."16\:00".$nom2."17\:00.csv";
  26. my $S18 = $nom1.$nom2."17\:00".$nom2."18\:00.csv";
  27. my $S19 = $nom1.$nom2."18\:00".$nom2."19\:00.csv";
  28. my $S20 = $nom1.$nom2."19\:00".$nom2."20\:00.csv";
  29. my $S21 = $nom1.$nom2."20\:00".$nom2."21\:00.csv";
  30. my $S22 = $nom1.$nom2."21\:00".$nom2."22\:00.csv";
  31. my $S23 = $nom1.$nom2."22\:00".$nom2."23\:00.csv";
  32. my $S24 = $nom1.$nom2."23\:00".$nom3."00\:00.csv";
  33.  
  34.  
  35. open my $FORMAT_FONT, '<', $nombre_arch_fuen2;
  36.  
  37. open my $FD1, '>', $S1;
  38. open my $FD2, '>', $S2;
  39. open my $FD3, '>', $S3;
  40. open my $FD4, '>', $S4;
  41. open my $FD5, '>', $S5;
  42. open my $FD6, '>', $S6;
  43. open my $FD7, '>', $S7;
  44. open my $FD8, '>', $S8;
  45. open my $FD9, '>', $S9;
  46. open my $FD10, '>', $S10;
  47. open my $FD11, '>', $S11;
  48. open my $FD12, '>', $S12;
  49. open my $FD13, '>', $S13;
  50. open my $FD14, '>', $S14;
  51. open my $FD15, '>', $S15;
  52. open my $FD16, '>', $S16;
  53. open my $FD17, '>', $S17;
  54. open my $FD18, '>', $S18;
  55. open my $FD19, '>', $S19;
  56. open my $FD20, '>', $S20;
  57. open my $FD21, '>', $S21;
  58. open my $FD22, '>', $S22;
  59. open my $FD23, '>', $S23;
  60. open my $FD24, '>', $S24;
  61.  
  62. chmod 0777, $S1;
  63. chmod 0777, $S2;
  64. chmod 0777, $S3;
  65. chmod 0777, $S4;
  66. chmod 0777, $S5;
  67. chmod 0777, $S6;
  68. chmod 0777, $S7;
  69. chmod 0777, $S8;
  70. chmod 0777, $S9;
  71. chmod 0777, $S10;
  72. chmod 0777, $S11;
  73. chmod 0777, $S12;
  74. chmod 0777, $S13;
  75. chmod 0777, $S14;
  76. chmod 0777, $S15;
  77. chmod 0777, $S16;
  78. chmod 0777, $S17;
  79. chmod 0777, $S18;
  80. chmod 0777, $S19;
  81. chmod 0777, $S20;
  82. chmod 0777, $S21;
  83. chmod 0777, $S22;
  84. chmod 0777, $S23;
  85. chmod 0777, $S24;
  86.  
  87. # agrego una cabecera a cada uno
  88.  
  89. print $FD1 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  90. print $FD2 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  91. print $FD3 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  92. print $FD4 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  93. print $FD5 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  94. print $FD6 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  95. print $FD7 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  96. print $FD8 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  97. print $FD9 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  98. print $FD10 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  99. print $FD11 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  100. print $FD12 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  101. print $FD13 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  102. print $FD14 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  103. print $FD15 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  104. print $FD16 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  105. print $FD17 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  106. print $FD18 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  107. print $FD19 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  108. print $FD20 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  109. print $FD21 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  110. print $FD22 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  111. print $FD23 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  112. print $FD24 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  113.  
  114.  
  115.  
  116.  
  117. my $i = 0 ;
  118.  
  119. # extraigo el contenido del original y según sea el registro de la hora lo mando al archivo correspondiente
  120. while (my $registro = <$FORMAT_FONT>){
  121. next if $. == 1 ;
  122. my @campos = quotewords (",",0, $registro ) ;
  123. $campos[1] = qq("$campos[1]");
  124. my $fecha = substr ($campos[0],11,2);
  125. my $ini = substr($campos[0],0,14);
  126. $campos[0] = $ini."01";
  127.  
  128. given ( $fecha )
  129. {
  130.  
  131. when ( '00'){
  132. my $archivo_final = join ',',@campos[0,1,2,3,4];
  133. print $FD1 "$archivo_final";
  134. }
  135.  
  136. when ( '01'){
  137. my $archivo_final = join ',',@campos[0,1,2,3,4];
  138. print $FD2 "$archivo_final";
  139. }
  140.  
  141. when ( '02'){
  142. my $archivo_final = join ',',@campos[0,1,2,3,4];
  143. print $FD3 "$archivo_final";
  144. }
  145.  
  146. when ( '03'){
  147. my $archivo_final = join ',',@campos[0,1,2,3,4];
  148. print $FD4 "$archivo_final";
  149. }
  150.  
  151. when ( '04'){
  152. my $archivo_final = join ',',@campos[0,1,2,3,4];
  153. print $FD5 "$archivo_final";
  154. }
  155.  
  156. when ( '05'){
  157. my $archivo_final = join ',',@campos[0,1,2,3,4];
  158. print $FD6 "$archivo_final";
  159. }
  160.  
  161. when ( '06'){
  162. my $archivo_final = join ',',@campos[0,1,2,3,4];
  163. print $FD7 "$archivo_final";
  164. }
  165.  
  166. when ( '07'){
  167. my $archivo_final = join ',',@campos[0,1,2,3,4];
  168. print $FD8 "$archivo_final";
  169. }
  170.  
  171. when ( '08'){
  172. my $archivo_final = join ',',@campos[0,1,2,3,4];
  173. print $FD9 "$archivo_final";
  174. }
  175.  
  176. when ( '09'){
  177. my $archivo_final = join ',',@campos[0,1,2,3,4];
  178. print $FD10 "$archivo_final";
  179. }
  180.  
  181. when ( '10'){
  182. my $archivo_final = join ',',@campos[0,1,2,3,4];
  183. print $FD11 "$archivo_final";
  184. }
  185.  
  186. when ( '11'){
  187. my $archivo_final = join ',',@campos[0,1,2,3,4];
  188. print $FD12 "$archivo_final";
  189. }
  190.  
  191. when ( '12'){
  192. my $archivo_final = join ',',@campos[0,1,2,3,4];
  193. print $FD13 "$archivo_final";
  194. }
  195.  
  196. when ( '13'){
  197. my $archivo_final = join ',',@campos[0,1,2,3,4];
  198. print $FD14 "$archivo_final";
  199. }
  200.  
  201. when ( '14'){
  202. my $archivo_final = join ',',@campos[0,1,2,3,4];
  203. print $FD15 "$archivo_final";
  204. }
  205.  
  206. when ( '15'){
  207. my $archivo_final = join ',',@campos[0,1,2,3,4];
  208. print $FD16 "$archivo_final";
  209. }
  210.  
  211. when ( '16'){
  212. my $archivo_final = join ',',@campos[0,1,2,3,4];
  213. print $FD17 "$archivo_final";
  214. }
  215.  
  216. when ( '17'){
  217. my $archivo_final = join ',',@campos[0,1,2,3,4];
  218. print $FD18 "$archivo_final";
  219. }
  220.  
  221. when ( '18'){
  222. my $archivo_final = join ',',@campos[0,1,2,3,4];
  223. print $FD19 "$archivo_final";
  224. }
  225.  
  226. when ( '19'){
  227. my $archivo_final = join ',',@campos[0,1,2,3,4];
  228. print $FD20 "$archivo_final";
  229. }
  230.  
  231. when ( '20'){
  232. my $archivo_final = join ',',@campos[0,1,2,3,4];
  233. print $FD21 "$archivo_final";
  234. }
  235.  
  236. when ( '21'){
  237. my $archivo_final = join ',',@campos[0,1,2,3,4];
  238. print $FD22 "$archivo_final";
  239. }
  240.  
  241. when ( '22'){
  242. my $archivo_final = join ',',@campos[0,1,2,3,4];
  243. print $FD23 "$archivo_final";
  244. }
  245.  
  246. when ( '23'){
  247. my $archivo_final = join ',',@campos[0,1,2,3,4];
  248. print $FD24 "$archivo_final";
  249. }
  250.  
  251. }
  252. $i++;
  253. }
  254.  
  255. close $FORMAT_FONT;
  256. close $S1;
  257. close $S2;
  258. close $S3;
  259. close $S4;
  260. close $S5;
  261. close $S6;
  262. close $S7;
  263. close $S8;
  264. close $S9;
  265. close $S10;
  266. close $S11;
  267. close $S12;
  268. close $S13;
  269. close $S14;
  270. close $S15;
  271. close $S16;
  272. close $S17;
  273. close $S18;
  274. close $S19;
  275. close $S20;
  276. close $S21;
  277. close $S22;
  278. close $S23;
  279. close $S24;
  280.  
  281. print "$i \n";
  282.  
Coloreado en 0.009 segundos, usando GeSHi 1.0.8.4


Bueno hasta ahí todo bien :D, seguro se puede optimizar ese código pero igual me funciona así.

EL PROBLEMA: es que tengo que aplicar ese código una infinidad de archivos que se encuentran en un directorio y pensé en que este mismo código podía recorrer el directorio y aplicarlo a archivo por archivo.

Por eso le hice unas ligeras modificaciones así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. #
  3.  
  4.  
  5. my $dir = "/home/scadmin/data_M2000/prueba/"; # es  otro directorio
  6. my $e = 0 ;
  7.  
  8. # abro un opendir para la lectura del directorio
  9.  
  10. opendir(DIR,$dir);
  11. my @FILES = grep(!/^\./,readdir(DIR));
  12.  
  13. my $j=0 ;
  14.  
  15. foreach $file (@FILES){
  16.      $j++;
  17. }
  18.  
  19. my $e = 0 ;
  20.  
  21. while (my $nombre_arch_fuen2 = $FILES[$e])
  22.  
  23. {
  24. my $nom1 = substr($nombre_arch_fuen2,0,3);
  25. my $nom2 = substr($nombre_arch_fuen2,3,12);
  26. my $nom3 = substr($nombre_arch_fuen2,20,12);
  27.  
  28. my $S1 = $nom1.$nom2."00\:00".$nom2."01\:00.csv";
  29. my $S2 = $nom1.$nom2."01\:00".$nom2."02\:00.csv";
  30. my $S3 = $nom1.$nom2."02\:00".$nom2."03\:00.csv";
  31. my $S4 = $nom1.$nom2."03\:00".$nom2."04\:00.csv";
  32. my $S5 = $nom1.$nom2."04\:00".$nom2."05\:00.csv";
  33. my $S6 = $nom1.$nom2."05\:00".$nom2."06\:00.csv";
  34. my $S7 = $nom1.$nom2."06\:00".$nom2."07\:00.csv";
  35. my $S8 = $nom1.$nom2."07\:00".$nom2."08\:00.csv";
  36. my $S9 = $nom1.$nom2."08\:00".$nom2."09\:00.csv";
  37. my $S10 = $nom1.$nom2."09\:00".$nom2."10\:00.csv";
  38. my $S11 = $nom1.$nom2."10\:00".$nom2."11\:00.csv";
  39. my $S12 = $nom1.$nom2."11\:00".$nom2."12\:00.csv";
  40. my $S13 = $nom1.$nom2."12\:00".$nom2."13\:00.csv";
  41. my $S14 = $nom1.$nom2."13\:00".$nom2."14\:00.csv";
  42. my $S15 = $nom1.$nom2."14\:00".$nom2."15\:00.csv";
  43. my $S16 = $nom1.$nom2."15\:00".$nom2."16\:00.csv";
  44. my $S17 = $nom1.$nom2."16\:00".$nom2."17\:00.csv";
  45. my $S18 = $nom1.$nom2."17\:00".$nom2."18\:00.csv";
  46. my $S19 = $nom1.$nom2."18\:00".$nom2."19\:00.csv";
  47. my $S20 = $nom1.$nom2."19\:00".$nom2."20\:00.csv";
  48. my $S21 = $nom1.$nom2."20\:00".$nom2."21\:00.csv";
  49. my $S22 = $nom1.$nom2."21\:00".$nom2."22\:00.csv";
  50. my $S23 = $nom1.$nom2."22\:00".$nom2."23\:00.csv";
  51. my $S24 = $nom1.$nom2."23\:00".$nom3."00\:00.csv";
  52.  
  53.  
  54. open my $FORMAT_FONT, '<', $nombre_arch_fuen2;
  55.  
  56. open my $FD1, '>', $S1;
  57. open my $FD2, '>', $S2;
  58. open my $FD3, '>', $S3;
  59. open my $FD4, '>', $S4;
  60. open my $FD5, '>', $S5;
  61. open my $FD6, '>', $S6;
  62. open my $FD7, '>', $S7;
  63. open my $FD8, '>', $S8;
  64. open my $FD9, '>', $S9;
  65. open my $FD10, '>', $S10;
  66. open my $FD11, '>', $S11;
  67. open my $FD12, '>', $S12;
  68. open my $FD13, '>', $S13;
  69. open my $FD14, '>', $S14;
  70. open my $FD15, '>', $S15;
  71. open my $FD16, '>', $S16;
  72. open my $FD17, '>', $S17;
  73. open my $FD18, '>', $S18;
  74. open my $FD19, '>', $S19;
  75. open my $FD20, '>', $S20;
  76. open my $FD21, '>', $S21;
  77. open my $FD22, '>', $S22;
  78. open my $FD23, '>', $S23;
  79. open my $FD24, '>', $S24;
  80.  
  81. chmod 0777, $S1;
  82. chmod 0777, $S2;
  83. chmod 0777, $S3;
  84. chmod 0777, $S4;
  85. chmod 0777, $S5;
  86. chmod 0777, $S6;
  87. chmod 0777, $S7;
  88. chmod 0777, $S8;
  89. chmod 0777, $S9;
  90. chmod 0777, $S10;
  91. chmod 0777, $S11;
  92. chmod 0777, $S12;
  93. chmod 0777, $S13;
  94. chmod 0777, $S14;
  95. chmod 0777, $S15;
  96. chmod 0777, $S16;
  97. chmod 0777, $S17;
  98. chmod 0777, $S18;
  99. chmod 0777, $S19;
  100. chmod 0777, $S20;
  101. chmod 0777, $S21;
  102. chmod 0777, $S22;
  103. chmod 0777, $S23;
  104. chmod 0777, $S24;
  105.  
  106.  
  107. print $FD1 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  108. print $FD2 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  109. print $FD3 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  110. print $FD4 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  111. print $FD5 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  112. print $FD6 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  113. print $FD7 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  114. print $FD8 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  115. print $FD9 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  116. print $FD10 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  117. print $FD11 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  118. print $FD12 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  119. print $FD13 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  120. print $FD14 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  121. print $FD15 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  122. print $FD16 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  123. print $FD17 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  124. print $FD18 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  125. print $FD19 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  126. print $FD20 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  127. print $FD21 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  128. print $FD22 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  129. print $FD23 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  130. print $FD24 "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  131.  
  132.  
  133.  
  134.  
  135. my $i = 0 ;
  136.  
  137. while (my $registro = <$FORMAT_FONT>){
  138. next if $. == 1 ;
  139. my @campos = quotewords (",",0, $registro ) ;
  140. $campos[1] = qq("$campos[1]");
  141. my $fecha = substr ($campos[0],11,2);
  142.  
  143.  
  144. given ( $fecha )
  145. {
  146.  
  147. when ( '00'){
  148. my $archivo_final = join ',',@campos[0,1,2,3,4];
  149. print $FD1 "$archivo_final";
  150. }
  151.  
  152. when ( '01'){
  153. my $archivo_final = join ',',@campos[0,1,2,3,4];
  154. print $FD2 "$archivo_final";
  155. }
  156.  
  157. when ( '02'){
  158. my $archivo_final = join ',',@campos[0,1,2,3,4];
  159. print $FD3 "$archivo_final";
  160. }
  161.  
  162. when ( '03'){
  163. my $archivo_final = join ',',@campos[0,1,2,3,4];
  164. print $FD4 "$archivo_final";
  165. }
  166.  
  167. when ( '04'){
  168. my $archivo_final = join ',',@campos[0,1,2,3,4];
  169. print $FD5 "$archivo_final";
  170. }
  171.  
  172. when ( '05'){
  173. my $archivo_final = join ',',@campos[0,1,2,3,4];
  174. print $FD6 "$archivo_final";
  175. }
  176.  
  177. when ( '06'){
  178. my $archivo_final = join ',',@campos[0,1,2,3,4];
  179. print $FD7 "$archivo_final";
  180. }
  181.  
  182. when ( '07'){
  183. my $archivo_final = join ',',@campos[0,1,2,3,4];
  184. print $FD8 "$archivo_final";
  185. }
  186.  
  187. when ( '08'){
  188. my $archivo_final = join ',',@campos[0,1,2,3,4];
  189. print $FD9 "$archivo_final";
  190. }
  191.  
  192. when ( '09'){
  193. my $archivo_final = join ',',@campos[0,1,2,3,4];
  194. print $FD10 "$archivo_final";
  195. }
  196.  
  197. when ( '10'){
  198. my $archivo_final = join ',',@campos[0,1,2,3,4];
  199. print $FD11 "$archivo_final";
  200. }
  201.  
  202. when ( '11'){
  203. my $archivo_final = join ',',@campos[0,1,2,3,4];
  204. print $FD12 "$archivo_final";
  205. }
  206.  
  207. when ( '12'){
  208. my $archivo_final = join ',',@campos[0,1,2,3,4];
  209. print $FD13 "$archivo_final";
  210. }
  211.  
  212. when ( '13'){
  213. my $archivo_final = join ',',@campos[0,1,2,3,4];
  214. print $FD14 "$archivo_final";
  215. }
  216.  
  217. when ( '14'){
  218. my $archivo_final = join ',',@campos[0,1,2,3,4];
  219. print $FD15 "$archivo_final";
  220. }
  221.  
  222. when ( '15'){
  223. my $archivo_final = join ',',@campos[0,1,2,3,4];
  224. print $FD16 "$archivo_final";
  225. }
  226.  
  227. when ( '16'){
  228. my $archivo_final = join ',',@campos[0,1,2,3,4];
  229. print $FD17 "$archivo_final";
  230. }
  231.  
  232. when ( '17'){
  233. my $archivo_final = join ',',@campos[0,1,2,3,4];
  234. print $FD18 "$archivo_final";
  235. }
  236.  
  237. when ( '18'){
  238. my $archivo_final = join ',',@campos[0,1,2,3,4];
  239. print $FD19 "$archivo_final";
  240. }
  241.  
  242. when ( '19'){
  243. my $archivo_final = join ',',@campos[0,1,2,3,4];
  244. print $FD20 "$archivo_final";
  245. }
  246.  
  247. when ( '20'){
  248. my $archivo_final = join ',',@campos[0,1,2,3,4];
  249. print $FD21 "$archivo_final";
  250. }
  251.  
  252. when ( '21'){
  253. my $archivo_final = join ',',@campos[0,1,2,3,4];
  254. print $FD22 "$archivo_final";
  255. }
  256.  
  257. when ( '22'){
  258. my $archivo_final = join ',',@campos[0,1,2,3,4];
  259. print $FD23 "$archivo_final";
  260. }
  261.  
  262. when ( '23'){
  263. my $archivo_final = join ',',@campos[0,1,2,3,4];
  264. print $FD24 "$archivo_final";
  265. }
  266.  
  267. }
  268. $i++;
  269. }
  270.  
  271. close $FORMAT_FONT;
  272. close $S1;
  273. close $S2;
  274. close $S3;
  275. close $S4;
  276. close $S5;
  277. close $S6;
  278. close $S7;
  279. close $S8;
  280. close $S9;
  281. close $S10;
  282. close $S11;
  283. close $S12;
  284. close $S13;
  285. close $S14;
  286. close $S15;
  287. close $S16;
  288. close $S17;
  289. close $S18;
  290. close $S19;
  291. close $S20;
  292. close $S21;
  293. close $S22;
  294. close $S23;
  295. close $S24;
  296.  
  297.  
  298. if ( $e <  $j )
  299. {$e++;
  300. }
  301. else
  302. {
  303. print "se acabo \n";
  304. }
  305.  
  306. }
  307.        
  308. closedir(DIR);
  309.  
  310. print "cantidad de archivos procesados $e \n";
  311.  
Coloreado en 0.007 segundos, usando GeSHi 1.0.8.4


Bueno, el código me funciona parcialmente, porque genera los archivos con los nombres correctos PERO no guarda el contenido del archivo original en los nuevos y posiblemente no lo esté extrayendo.

Espero me hayan entendido y puedan ayudarme.

¡Gracias!

JZ
JoshZeta
Perlero nuevo
Perlero nuevo
 
Mensajes: 13
Registrado: 2015-08-20 11:40 @527

Publicidad

Re: Recorrido de ficheros y directorio

Notapor explorer » 2016-01-27 07:04 @336

¡Qué largo!

Las operaciones de escritura se hacen dentro de la estructura given/when, así que si no se está produciendo ninguna salida, $fecha contiene un valor que no coincide con ninguna de las entradas when().

Puedes agregar una entrada más, llamada "por defecto":

default { say "ERROR. Valor de \$fecha: [$fecha]" }

dentro del given() y así sabes si ocurre algún caso excepcional.


Aquí está mi versión corta:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use v5.10;
  3. use autodie;
  4. use File::Basename;
  5. use Text::ParseWords;
  6.  
  7. # /home/scadmin/data_M2000/PDP/PDP_2016-01-21 00:00_2016-01-22 00:00.csv
  8. # 0         10        20        30        40        50            
  9.  
  10. #my $nombre_arch_fuen2 = shift; # nombre de archivo pasado por la línea de comando, como argumento
  11. my $nombre_arch_fuen2 = "/home/scadmin/data_M2000/PDP/PDP_2016-01-21 00:00_2016-01-22 00:00.csv"; # manualmente
  12.  
  13. my($nombre_archivo, $ruta) = fileparse($nombre_arch_fuen2);
  14.  
  15. my($sistema, $fecha1, $fecha2) = $nombre_archivo =~ /^(\w+?)_(\d{4}-\d{2}-\d{2}) 00:00_((?-2))/;
  16.  
  17. # Creación de los archivos
  18. my @FD;
  19.  
  20. for my $hora ( '00' .. '23' ) {
  21.  
  22.     my $siguiente = sprintf "%02d", ($hora + 1) % 24;
  23.  
  24.     my $archivo = "${sistema}_$fecha1 $hora:00_$fecha1 $siguiente:00.csv";
  25.  
  26.     open $FD[$hora], '>', $archivo;
  27.  
  28.     chmod 0777, $archivo;
  29.  
  30.     print {$FD[$hora]} "Result Time,Object Name,Current activated PDP contexts,PDP context activation requests,PDP context activation success ratio\n";
  31. }
  32.  
  33. # extraigo el contenido del original y según sea el registro de la hora lo mando al archivo correspondiente
  34. open my $FORMAT_FONT, '<', $nombre_arch_fuen2;
  35. my $i = 0 ;
  36.  
  37. while (my $registro = <$FORMAT_FONT>) {
  38.     next if $. == 1 ;                                   # saltamos la cabecera
  39.  
  40.     my @campos = quotewords (",", 0, $registro);
  41.  
  42.     $campos[1] = qq("$campos[1]");
  43.     my $fecha  = substr($campos[0], 11, 2);
  44.     my $ini    = substr($campos[0],  0,14);
  45.     $campos[0] = $ini."01";
  46.  
  47.     print {$FD[$fecha]} join ',', @campos[0..4];
  48.  
  49.     $i++;
  50. }
  51.  
  52. close $FORMAT_FONT;
  53.  
  54. close $FD[$_] for '00' .. '23';
  55.  
  56. print "$i\n";
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Recorrido de ficheros y directorio

Notapor JoshZeta » 2016-02-02 20:29 @895

¡¡¡Genial, explorer!!! :)

Usé tu versión del código con un foreach y ¡¡me funcionó de maravilla!!

Solo una acotación... Tú código no me funcionaba por la línea donde indicabas la versión v5.18 (creo que me versión es inferior). Y, adicionalmente, agregué la línea

use Text::ParseWords

para usar la función fileparse()

¡¡¡Gracias!!!
JoshZeta
Perlero nuevo
Perlero nuevo
 
Mensajes: 13
Registrado: 2015-08-20 11:40 @527

Re: Recorrido de ficheros y directorio

Notapor explorer » 2016-02-03 01:12 @092

La versión v5.18 es del año 2013. En unos meses sale v5.24.

Puedes cambiar el use v5.18 por cualquier versión que sea superior a v5.10.0, ya que hay una llamada a un say(), al final. O quitar el use y cambiar el say() por un print().

La función fileparse() se exporta desde File::basename, no desde Text::ParseWords, que no tiene ninguna función que se llame así. Supongo que te refieres a quotewords().

Ya lo corregí, gracias.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Recorrido de ficheros y directorio

Notapor JoshZeta » 2016-02-03 14:25 @642

Hola, explorer. Gracias por la aclaración.

Estaba revisando los archivos generados con el código nuevo y me percaté que la hora de la fecha de fin que figura en los nombres, cuando es el caso de un número de una cifra, no lo completa con un cero. Mejor dicho, la idea es que los nombres generados tengan el mismo tamaño, por eso yo los generaba así:

PDP_2015-09-01 00:00_2015-09-01 01:00.csv (tamaño 41)

pero tu código los genera así:

PDP_2015-09-01 00:00_2015-09-01 1:00.csv (tamaño 40).

En tu código. estas lineas generan ese nombre:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $archivo .= ( $HH != 23 )
  2.               ? ("$HH:00_$fecha1 " . ($HH+1) . ":00.csv")
  3.               : ("$HH:00_$fecha2 " .  "00"   . ":00.csv")
  4.               ;
  5.  
  6.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Pensé en agregarle un if() para no tener ese problema, hice lo siguiente:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $archivo .= ( $HH != 23 )
  2. if ( $HH > 8 ) {       
  3.     ? ("$HH:00_$fecha1 " . ($HH+1) . ":00.csv"): ("$HH:00_$fecha2 " .  "00"   . ":00.csv");
  4. }
  5. else {
  6.     ? ("$HH:00_$fecha1 " . "0".($HH+1) . ":00.csv"): ("$HH:00_$fecha2 " .  "00"   . ":00.csv");
  7. }
  8.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Pero me salen errores :(
JoshZeta
Perlero nuevo
Perlero nuevo
 
Mensajes: 13
Registrado: 2015-08-20 11:40 @527

Re: Recorrido de ficheros y directorio

Notapor JoshZeta » 2016-02-03 14:34 @648

Hola de nuevo. Ya lo resolví: El if() no estaba atrapando

$archivo .= ( $HH != 23 )

Me faltó eso, ¡gracias! :D
JoshZeta
Perlero nuevo
Perlero nuevo
 
Mensajes: 13
Registrado: 2015-08-20 11:40 @527

Re: Recorrido de ficheros y directorio

Notapor explorer » 2016-02-03 17:47 @783

Gracias por el aviso.

Ya arreglé el programa, cambiando el bucle de las líneas 20 a 31. Incluso ha quedado más corto ;)
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 13 invitados