Advertisement
cesarzeta

Reemplazador de texto

Sep 30th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.03 KB | None | 0 0
  1. #!/bin/bash
  2. # Busqueda y reemplazo de texto  en múltiples archivos. Modo gráfico con Zenity.
  3. # Autor César Zalazar. http://www.taringa.net/cesarzeta
  4. # Bajo licencia GPL
  5. ##############
  6. directorio=""
  7. tipo_archivo=""
  8. texto_anterior=""
  9. texto_nuevo="sin_asignar"
  10. function main(){
  11.     while [ true ]
  12.     do
  13.         opcion=`zenity --list --height=345 --width=520 --title "Buscar y reemplazar texto" --radiolist --column="Seleccionar" --column="Opción" TRUE "1 Seleccionar directorio " FALSE "2 Ingresar datos para el reemplazo"  FALSE "3 Informar y reemplazar" FALSE "4 Salir de la aplicación" `
  14.             seleccion `echo $opcion | cut -c 1` "$opcion"
  15.             if [ "$opcion" == ""  ]
  16.             then
  17.                 seleccion 4
  18.             fi
  19.     done
  20. }
  21. function seleccion(){
  22. case $1 in
  23. 1)
  24.         eliminar_temps
  25.         seleccionar_directorio
  26. ;;
  27. 2)
  28.         ingresar_datos
  29. ;;
  30. 3)
  31.         comprobar_datos
  32.         informar
  33. ;;
  34. 4)
  35.         eliminar_temps
  36. exit
  37. ;;
  38. esac
  39. }
  40. function seleccionar_directorio(){
  41.         FILE=`zenity --file-selection --title="Seleccione directorio de los archivos a modificar" --directory`
  42.                 cod=`echo $?`
  43.                if  [ $cod == 0 ]
  44.                then
  45.                                  directorio=`echo "$FILE"`
  46.                  zenity --info  --text="Se seleccionó el directorio "$directorio
  47.                  seleccion 2
  48.                elif  [ $cod == 1 ]
  49.                then
  50.                  zenity --warning --title="Selección de directorio" --text="No hay directorio seleccionado"
  51.                else
  52.                         zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
  53.                            fi
  54. }
  55.  
  56. function ingresar_datos(){
  57.         info_datos=`echo "Ningún campo puede estar vacío.\\n\\nComo ejemplo: ingrese el tipo de archivo con el formato *.txt . \\n\\nTexto anterior no puede ser  \"\" o usar comodines.  \\n\\nTexto nuevo no puede usar comodines y si es  \"\"  el texto a reemplazar sera borrado."`
  58.         zenity --info  --text="$info_datos"
  59.         datos=`zenity --forms --width=520 --title="Ingresar datos"  --text="Introduzca los datos para el reemplazo." --separator=";" --add-entry="Tipo de archivo a procesar" --add-entry="Texto anterior"  --add-entry="Texto nuevo" `
  60.         cod=`echo $?`
  61.               if [ $cod == 0 ]
  62.               then
  63.                 tipo_archivo=`echo $datos|cut -d';' -f1`
  64.                 texto_anterior=`echo $datos |cut -d';' -f2`
  65.                 texto_nuevo=`echo $datos |cut -d';' -f3 `  
  66.                 zenity --info  --text="Se cargaron los datos para el  reemplazo"
  67.                 seleccion 3
  68.               elif [ $cod == 1 ]
  69.               then
  70.                 zenity --warning --title="Ingreso de datos" --text="No se introdujeron los datos para el  reemplazo"
  71.               else
  72.                 zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
  73.               fi
  74. }
  75. function comprobar_datos(){
  76.         if  [ "$directorio" = ""  ]
  77.         then
  78.                 error_de_datos
  79.         fi
  80.         if  [  "$tipo_archivo" = "" ]
  81.         then
  82.                 error_de_datos
  83.         fi
  84.         if  [  "$texto_anterior" = "" ]
  85.         then
  86.                 error_de_datos
  87.         fi
  88.         if  [  "$texto_nuevo" = "sin_asignar" ]
  89.         then
  90.                 error_de_datos
  91.         fi
  92. }
  93. function error_de_datos(){
  94.         zenity --error --no-wrap --title="Error " --text="Datos incompletos o incorrectos"
  95.         zenity --info  --text="Ingrese nuevamente los datos necesarios"
  96.         if [ -f /tmp/archivos ]
  97.         then
  98.             rm /tmp/archivos
  99.         fi
  100.         if [ -f  /tmp/listarchivos ]
  101.         then
  102.             rm /tmp/listarchivos
  103.         fi
  104.         main
  105. }
  106. function informar(){
  107.     echo "Directorio seleccionado para reemplazos = "$directorio"" >> /tmp/archivos
  108.     echo "Tipo de archivo en los que se realizará el reemplazo = "$tipo_archivo"." >> /tmp/archivos
  109.         echo "Texto que será reemplazado = "$texto_anterior"." >> /tmp/archivos
  110.         echo "Texto que se usará en el reemplazo = "$texto_nuevo"." >> /tmp/archivos
  111.         echo "Archivos en los que se reemplazará "$texto_anterior" por "$texto_nuevo":"  >> /tmp/archivos
  112.         lista=`find "$directorio" -name "$tipo_archivo"`
  113.             if [ "$lista" == "" ]
  114.             then
  115.                 info_error=`echo "No hay archivos para reemplazo de texto.\\n\\n Revise la extensión ingresada o el contenido del directorio"`
  116.                 zenity --error --no-wrap --title="Error" --text="$info_error"
  117.                 main
  118.             fi
  119.     find "$directorio" -name "$tipo_archivo" >> /tmp/archivos
  120.     zenity --text-info --height=480 --width=640 --title="Información de cambios" --filename=/tmp/archivos
  121.     cod=`echo $?`
  122.               if [ $cod == 0 ]
  123.               then
  124.                  zenity --question --title="Advertencia" --text="¿Está seguro de proceder con el reemplazo?"
  125.                  cod=`echo $?`
  126.                         if [ $cod = 0 ]
  127.                         then   
  128.                             reemplazar
  129.                         else
  130.                             zenity --info  --text="Corrija lo que crea necesario"
  131.                             main
  132.                         fi
  133.              elif [ $cod == 1 ]
  134.              then
  135.                 zenity --info  --text="Corrija lo que crea necesario"
  136.                 main
  137.              else
  138.                 zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
  139.              fi
  140. }
  141. function reemplazar(){
  142.     info_reemplazar=`echo "Se crearán archivos de respaldo *.old.\\n\\nBórrelos si el reemplazo fue exitoso."`
  143.     zenity --info  --text="$info_reemplazar"
  144.     find "$directorio" -name  "$tipo_archivo" >> /tmp/listarchivos
  145.     while IFS= read LINE
  146.         do
  147.         fichero="$LINE"
  148.                 while IFS= read LINE
  149.                     do
  150.                     mi_fichero="$LINE"
  151.                     nueva_linea=`echo ${mi_fichero//$texto_anterior/$texto_nuevo}`
  152.                     echo "$nueva_linea"  >> "$fichero".new
  153.                 done < "$fichero"
  154.                     mv "$fichero" "$fichero".old
  155.                     mv "$fichero".new "$fichero"
  156.     done < /tmp/listarchivos  | progress
  157.     info_saliendo=`echo "Reemplazo completado.\\n\\n Saliendo de la aplicación."`
  158.     zenity --info  --text="$info_saliendo"
  159.     seleccion 4
  160. }
  161. function eliminar_temps(){
  162.     if [ -f /tmp/archivos ]
  163.     then
  164.         rm /tmp/archivos
  165.     fi
  166.     if [ -f  /tmp/listarchivos ]
  167.     then
  168.         rm /tmp/listarchivos
  169.     fi
  170.     zenity --info --text="Se borraron  ficheros temporales  de  la aplicación"
  171.         if [ $? == -1 ]
  172.         then
  173.             zenity --error --text="Error al eliminar archivos temporales."
  174.         fi
  175. }
  176. function progress(){
  177.     (
  178.     echo "0" ; sleep 1
  179.     echo "# $1" ; sleep 1
  180.     echo "25" ; sleep 1
  181.     echo "75" ; sleep 1
  182.     echo "100" ; sleep 1
  183.     echo "# Finalizado"
  184.     ) |
  185.     zenity --progress --title="Efectuando reemplazos " --text="" --percentage=0 --width=300
  186.     if [ "$?" != 0 ]
  187.     then
  188.         zenity --error --text="Operación Abortada."
  189.     fi
  190. }
  191. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement