Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #fichero : normalizador.sh
- #autor : Luis Carlos Mateos
- #resumen : Script en Bash que realiza la reparacion de las etiquetas de un fichero sgml
- tags=("<DOC>" "<ID>" "<TITLE>" "<CATEGORIES>" "<TAGS>" "<TEXT>")
- #formateamos el fichero , ya que los caracteres LF dan problemas, por eso los quitamos
- fich=./temp.sgml
- sourceFile=$1
- (cat $1 |tr -d '\012') > $fich #elimina todos los lf
- function recuperarTag()
- {
- local tag=$1
- local fich=$2
- #izda significa si tagpivote debe ir a la izquierda del tag perdido o a la derecha
- local izda=1
- for (( i=0; i <= ${#tags}; i++ )); do
- if [ "${tags[i]}" == "$tag" ]; then
- if [ $i -gt 0 ]; then
- izda=1
- if [ $i -eq 1 ];then
- let "i--"
- tagpivote=${tags[i]}
- else
- let "i--"
- tagpivote="</${tags[i]:1}"
- fi
- let "i++"
- else
- #falta <DOC>... por lo que hay que tagpivote=<ID>, tagpivote esta a la derecha
- izda=0
- tagpivote="<ID>"
- fi
- else
- if [ "</${tags[i]:1}" == "$tag" ];then
- if [ $i -gt 0 ]; then
- izda=0
- let aux=${#tags}
- if [ $i -eq $aux ]; then
- i=0
- tagpivote="</${tags[i]:1}"
- break
- else
- let "i++"
- tagpivote=${tags[i]}
- fi
- let "i--"
- else
- #falta </DOC>... por lo que tagpivote=</TEXT>
- izda=1
- tagpivote="</TEXT>"
- fi
- fi
- fi
- done
- if [ $izda -eq 1 ];then
- fixedfile=`cat $fich | sed 's#'"${tagpivote}"'#'"${tagpivote}${tag}"'#g'`
- else
- fixedfile=`cat $fich | sed 's#'"${tagpivote}"'#'"${tag}${tagpivote}"'#g'`
- fi
- echo "$fixedfile" > $fich
- }
- function repararTagsFichero()
- {
- tagnoexists=("")
- for tag in ${tags[*]} ;do
- tagz="</"${tag:1}
- tagexists=`grep -o "$tag" $fich`
- tagzexists=`grep -o "$tagz" $fich`
- if [ "$tagexists" == "" ]; then
- tagnoexists[${#tagnoexists[*]}]="$tag"
- fi
- if [ "$tagzexists" == "" ]; then
- tagnoexists[${#tagnoexists[*]}]="$tagz"
- fi
- done
- for tag in ${tagnoexists[*]} ;do
- recuperarTag $tag $fich
- done
- }
- function reformatearFichero()
- {
- fixedfile=`sed -e 's#\r##g' -e 's#</[^>]*>#&\r#g' -e 's#<DOC>#&\r#g' $fich`
- echo $fixedfile > $sourceFile
- rm $fich
- }
- function esValido()
- {
- for tag in ${tags[*]} ;do
- tagz="</"${tag:1}
- tagexists=`grep -o "$tag" $1`
- tagzexists=`grep -o "$tagz" $1`
- if [[ "$tagexists" == "" || "$tagzexists" == "" ]]; then
- echo "fichero erroneo"
- break
- fi
- done
- }
- repararTagsFichero
- reformatearFichero
- esValido $sourceFile
Add Comment
Please, Sign In to add comment