20090519

scripts para generar sentencias sql apartir de listados de archivos

Se tiene un directorio con 534 archivos en el formato CDMEXNCxxxxxyyyyy donde yyyyy son un numero de serie y xxxxx son etiquetas de operadores

$ ls -1 CD*| head
CDMEXNCARGNC03179
CDMEXNCARGNC03180
CDMEXNCARGNC03181
CDMEXNCARGNC03182
CDMEXNCAUSOP02976
CDMEXNCAUSOP02977
CDMEXNCAUSOP02978
CDMEXNCAUSOP02979
CDMEXNCAUTCA02374
CDMEXNCAUTCA02375

Estos datos fueron registrados en una tabla de BD, ¿se necesita consultar todos los registro? Solo entrega un registro por archivo.
La sentencia a emplear es

select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='ARGNC' and sqn=3179;

Para evitar el copiar cada segmento y pegarlo registro por registro

$ for _a in `ls -1 CD* | cut -c8-12 | sort -u`
> do
> echo "union">>file.sql;
> echo "select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='$_a' and sqn in \
> `ls -1 | grep $_a | cut -c13-17 | xargs | sed -e 's/ /,/g' -e 's/.*/(&)/'`" >> file.sql
> done
$ head file.sql
union
select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='ARGNC' and sqn in (03179,03180,03181,03182)
union
select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='AUSOP' and sqn in (02976,02977,02978,02979)
union
select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='AUTCA' and sqn in (02374,02375,02376,02377)
union
select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='AUTMM' and sqn in (03060,03061,03062,03063)
union
select vplmn, sqn, cre_date, name_fl, tot_reg from ixmed_d_roamtapout_sum where vplmn='AUTPT' and sqn in (02988,02989,02990,02991)

Ahora se puede emplear el archivo file.sql eliminando la primera linea para obtener el resultado. Si se cuenta la cantidad de registros que entrega en este caso es idéntico a la cantidad de archivos.

Explicación del código
  • `ls -1 CD* | cut -c8-12 | sort -u`
    • Se lista en una sola columna todos los archivos que inicien con CD, se cortan del caracter 8 al 12 y se ordenan eliminando duplicados
    • $ ls CD* | head
      CDMEXNCARGNC03179
      CDMEXNCARGNC03180
      CDMEXNCARGNC03181
      CDMEXNCARGNC03182
      CDMEXNCAUSOP02976
      CDMEXNCAUSOP02977
      CDMEXNCAUSOP02978
      CDMEXNCAUSOP02979
      CDMEXNCAUTCA02374
      CDMEXNCAUTCA02375
      $ ls CD* | cut -c8-12 | head
      ARGNC
      ARGNC
      ARGNC
      ARGNC
      AUSOP
      AUSOP
      AUSOP
      AUSOP
      AUTCA
      AUTCA
      $ ls CD* | cut -c8-12 | sort -u | head
      ARGNC
      AUSOP
      AUTCA
      AUTMM
      AUTPT
      AUTTR
      BELMO
      BELTB
      BGR01
      BHRBT
  • for _a in `ls -1 CD* | cut -c8-12 | sort -u`; do [...] done;
    • Se emplea un ciclo for para asignar a la variable $_a todas las cadenas que cumplan con el resultado del ls
  • Dentro de cada ciclo
    • Imprime union y lo manda al archivo file.sql
    • Imprime la sentencia concatenada con la salida del ls y lo envia al archivo file.sql
    • `ls -1 | grep $_a | cut -c13-17 | xargs | sed -e 's/ /,/g' -e 's/.*/(&)/'`
      • Se listan los archivos
      • Se busca el que tenga $_a en su nombre
      • Corta del caracter 13 al 17
      • Convierte la salida de una columna a una fila
      • Transforma todos los espacios en comas
      • Agrega a toda la cadena paréntesis al inicio y al final
      • $ ls -1 CD* | head
        CDMEXNCARGNC03179
        CDMEXNCARGNC03180
        CDMEXNCARGNC03181
        CDMEXNCARGNC03182
        CDMEXNCAUSOP02976
        CDMEXNCAUSOP02977
        CDMEXNCAUSOP02978
        CDMEXNCAUSOP02979
        CDMEXNCAUTCA02374
        CDMEXNCAUTCA02375
        $ ls -1 CD* | grep ARGNC
        CDMEXNCARGNC03179
        CDMEXNCARGNC03180
        CDMEXNCARGNC03181
        CDMEXNCARGNC03182
        $ ls -1 CD* | grep ARGNC | cut -c13-17
        03179
        03180
        03181
        03182
        $ ls -1 CD* | grep ARGNC | cut -c13-17 | xargs
        03179 03180 03181 03182
        $ ls -1 CD* | grep ARGNC | cut -c13-17 | xargs | sed -e 's/ /,/g'
        03179,03180,03181,03182
        $ ls -1 CD* | grep ARGNC | cut -c13-17 | xargs | sed -e 's/ /,/g' -e 's/.*/(&)/'
        (03179,03180,03181,03182)

Escrito con el Navegador Flock

20090511

SMTP prioridad alta

Cuando se maneja directamente en el protocolo SMTP y se requiere que el mensaje sea enviado con una prioridad elevada se puede aplicar la etiqueta


X-Priority: 1


Escrito con el Navegador Flock