Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Busqueda y reemplazo de texto en múltiples archivos. Modo gráfico con Zenity.
- # Autor César Zalazar. http://www.taringa.net/cesarzeta
- # Bajo licencia GPL
- ##############
- directorio=""
- tipo_archivo=""
- texto_anterior=""
- texto_nuevo="sin_asignar"
- function main(){
- while [ true ]
- do
- 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" `
- seleccion `echo $opcion | cut -c 1` "$opcion"
- if [ "$opcion" == "" ]
- then
- seleccion 4
- fi
- done
- }
- function seleccion(){
- case $1 in
- 1)
- eliminar_temps
- seleccionar_directorio
- ;;
- 2)
- ingresar_datos
- ;;
- 3)
- comprobar_datos
- informar
- ;;
- 4)
- eliminar_temps
- exit
- ;;
- esac
- }
- function seleccionar_directorio(){
- FILE=`zenity --file-selection --title="Seleccione directorio de los archivos a modificar" --directory`
- cod=`echo $?`
- if [ $cod == 0 ]
- then
- directorio=`echo "$FILE"`
- zenity --info --text="Se seleccionó el directorio "$directorio
- seleccion 2
- elif [ $cod == 1 ]
- then
- zenity --warning --title="Selección de directorio" --text="No hay directorio seleccionado"
- else
- zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
- fi
- }
- function ingresar_datos(){
- 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."`
- zenity --info --text="$info_datos"
- 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" `
- cod=`echo $?`
- if [ $cod == 0 ]
- then
- tipo_archivo=`echo $datos|cut -d';' -f1`
- texto_anterior=`echo $datos |cut -d';' -f2`
- texto_nuevo=`echo $datos |cut -d';' -f3 `
- zenity --info --text="Se cargaron los datos para el reemplazo"
- seleccion 3
- elif [ $cod == 1 ]
- then
- zenity --warning --title="Ingreso de datos" --text="No se introdujeron los datos para el reemplazo"
- else
- zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
- fi
- }
- function comprobar_datos(){
- if [ "$directorio" = "" ]
- then
- error_de_datos
- fi
- if [ "$tipo_archivo" = "" ]
- then
- error_de_datos
- fi
- if [ "$texto_anterior" = "" ]
- then
- error_de_datos
- fi
- if [ "$texto_nuevo" = "sin_asignar" ]
- then
- error_de_datos
- fi
- }
- function error_de_datos(){
- zenity --error --no-wrap --title="Error " --text="Datos incompletos o incorrectos"
- zenity --info --text="Ingrese nuevamente los datos necesarios"
- if [ -f /tmp/archivos ]
- then
- rm /tmp/archivos
- fi
- if [ -f /tmp/listarchivos ]
- then
- rm /tmp/listarchivos
- fi
- main
- }
- function informar(){
- echo "Directorio seleccionado para reemplazos = "$directorio"" >> /tmp/archivos
- echo "Tipo de archivo en los que se realizará el reemplazo = "$tipo_archivo"." >> /tmp/archivos
- echo "Texto que será reemplazado = "$texto_anterior"." >> /tmp/archivos
- echo "Texto que se usará en el reemplazo = "$texto_nuevo"." >> /tmp/archivos
- echo "Archivos en los que se reemplazará "$texto_anterior" por "$texto_nuevo":" >> /tmp/archivos
- lista=`find "$directorio" -name "$tipo_archivo"`
- if [ "$lista" == "" ]
- then
- info_error=`echo "No hay archivos para reemplazo de texto.\\n\\n Revise la extensión ingresada o el contenido del directorio"`
- zenity --error --no-wrap --title="Error" --text="$info_error"
- main
- fi
- find "$directorio" -name "$tipo_archivo" >> /tmp/archivos
- zenity --text-info --height=480 --width=640 --title="Información de cambios" --filename=/tmp/archivos
- cod=`echo $?`
- if [ $cod == 0 ]
- then
- zenity --question --title="Advertencia" --text="¿Está seguro de proceder con el reemplazo?"
- cod=`echo $?`
- if [ $cod = 0 ]
- then
- reemplazar
- else
- zenity --info --text="Corrija lo que crea necesario"
- main
- fi
- elif [ $cod == 1 ]
- then
- zenity --info --text="Corrija lo que crea necesario"
- main
- else
- zenity --error --no-wrap --title="Error" --text="Ocurrió un error"
- fi
- }
- function reemplazar(){
- info_reemplazar=`echo "Se crearán archivos de respaldo *.old.\\n\\nBórrelos si el reemplazo fue exitoso."`
- zenity --info --text="$info_reemplazar"
- find "$directorio" -name "$tipo_archivo" >> /tmp/listarchivos
- while IFS= read LINE
- do
- fichero="$LINE"
- while IFS= read LINE
- do
- mi_fichero="$LINE"
- nueva_linea=`echo ${mi_fichero//$texto_anterior/$texto_nuevo}`
- echo "$nueva_linea" >> "$fichero".new
- done < "$fichero"
- mv "$fichero" "$fichero".old
- mv "$fichero".new "$fichero"
- done < /tmp/listarchivos | progress
- info_saliendo=`echo "Reemplazo completado.\\n\\n Saliendo de la aplicación."`
- zenity --info --text="$info_saliendo"
- seleccion 4
- }
- function eliminar_temps(){
- if [ -f /tmp/archivos ]
- then
- rm /tmp/archivos
- fi
- if [ -f /tmp/listarchivos ]
- then
- rm /tmp/listarchivos
- fi
- zenity --info --text="Se borraron ficheros temporales de la aplicación"
- if [ $? == -1 ]
- then
- zenity --error --text="Error al eliminar archivos temporales."
- fi
- }
- function progress(){
- (
- echo "0" ; sleep 1
- echo "# $1" ; sleep 1
- echo "25" ; sleep 1
- echo "75" ; sleep 1
- echo "100" ; sleep 1
- echo "# Finalizado"
- ) |
- zenity --progress --title="Efectuando reemplazos " --text="" --percentage=0 --width=300
- if [ "$?" != 0 ]
- then
- zenity --error --text="Operación Abortada."
- fi
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement