Guest User

Untitled

a guest
Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #fichero : normalizador.sh
  4. #autor : Luis Carlos Mateos
  5. #resumen : Script en Bash que realiza la reparacion de las etiquetas de un fichero sgml
  6.  
  7. tags=("<DOC>" "<ID>" "<TITLE>" "<CATEGORIES>" "<TAGS>" "<TEXT>")
  8.  
  9. #formateamos el fichero , ya que los caracteres LF dan problemas, por eso los quitamos
  10. fich=./temp.sgml
  11. sourceFile=$1
  12. (cat $1 |tr -d '\012') > $fich #elimina todos los lf
  13.  
  14. function recuperarTag()
  15. {
  16. local tag=$1
  17. local fich=$2
  18. #izda significa si tagpivote debe ir a la izquierda del tag perdido o a la derecha
  19. local izda=1
  20.  
  21. for (( i=0; i <= ${#tags}; i++ )); do
  22.  
  23. if [ "${tags[i]}" == "$tag" ]; then
  24. if [ $i -gt 0 ]; then
  25. izda=1
  26.  
  27. if [ $i -eq 1 ];then
  28. let "i--"
  29. tagpivote=${tags[i]}
  30. else
  31. let "i--"
  32. tagpivote="</${tags[i]:1}"
  33. fi
  34.  
  35. let "i++"
  36. else
  37. #falta <DOC>... por lo que hay que tagpivote=<ID>, tagpivote esta a la derecha
  38. izda=0
  39. tagpivote="<ID>"
  40. fi
  41. else
  42. if [ "</${tags[i]:1}" == "$tag" ];then
  43.  
  44. if [ $i -gt 0 ]; then
  45. izda=0
  46. let aux=${#tags}
  47. if [ $i -eq $aux ]; then
  48. i=0
  49. tagpivote="</${tags[i]:1}"
  50. break
  51. else
  52. let "i++"
  53. tagpivote=${tags[i]}
  54. fi
  55.  
  56. let "i--"
  57.  
  58. else
  59. #falta </DOC>... por lo que tagpivote=</TEXT>
  60. izda=1
  61. tagpivote="</TEXT>"
  62. fi
  63. fi
  64. fi
  65.  
  66. done
  67.  
  68. if [ $izda -eq 1 ];then
  69. fixedfile=`cat $fich | sed 's#'"${tagpivote}"'#'"${tagpivote}${tag}"'#g'`
  70. else
  71. fixedfile=`cat $fich | sed 's#'"${tagpivote}"'#'"${tag}${tagpivote}"'#g'`
  72. fi
  73. echo "$fixedfile" > $fich
  74.  
  75. }
  76.  
  77. function repararTagsFichero()
  78. {
  79. tagnoexists=("")
  80. for tag in ${tags[*]} ;do
  81.  
  82. tagz="</"${tag:1}
  83. tagexists=`grep -o "$tag" $fich`
  84. tagzexists=`grep -o "$tagz" $fich`
  85.  
  86. if [ "$tagexists" == "" ]; then
  87. tagnoexists[${#tagnoexists[*]}]="$tag"
  88. fi
  89.  
  90. if [ "$tagzexists" == "" ]; then
  91. tagnoexists[${#tagnoexists[*]}]="$tagz"
  92. fi
  93. done
  94.  
  95. for tag in ${tagnoexists[*]} ;do
  96. recuperarTag $tag $fich
  97. done
  98. }
  99.  
  100. function reformatearFichero()
  101. {
  102. fixedfile=`sed -e 's#\r##g' -e 's#</[^>]*>#&\r#g' -e 's#<DOC>#&\r#g' $fich`
  103. echo $fixedfile > $sourceFile
  104. rm $fich
  105. }
  106.  
  107. function esValido()
  108. {
  109. for tag in ${tags[*]} ;do
  110.  
  111. tagz="</"${tag:1}
  112. tagexists=`grep -o "$tag" $1`
  113. tagzexists=`grep -o "$tagz" $1`
  114.  
  115. if [[ "$tagexists" == "" || "$tagzexists" == "" ]]; then
  116. echo "fichero erroneo"
  117. break
  118. fi
  119. done
  120. }
  121.  
  122.  
  123. repararTagsFichero
  124. reformatearFichero
  125. esValido $sourceFile
Add Comment
Please, Sign In to add comment