PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/0.1/Algebre/script-quest1-pl

http://prolgebra.googlecode.com/
Korn Shell | 91 lines | 43 code | 7 blank | 41 comment | 6 complexity | e4825ec08fe81ee3ef191f8469431d47 MD5 | raw file
  1. #!/usr/bin/ksh
  2. #
  3. # SCRIPT: 12_ways_to_parse.ksh.ksh
  4. #
  5. #
  6. # REV: 1.2.A
  7. #
  8. # PURPOSE: This script shows the different ways of reading
  9. # a file line by line. Again there is not just one way
  10. # to read a file line by line and some are faster than
  11. # others and some are more intuitive than others.
  12. #
  13. # REV LIST:
  14. #
  15. # 03/15/2002 - Randy Michael
  16. # Set each of the while loops up as functions and the timing
  17. # of each function to see which one is the fastest.
  18. #
  19. #######################################################################
  20. #
  21. # NOTE: To output the timing to a file use the following syntax:
  22. #
  23. # 12_ways_to_parse.ksh file_to_process > output_file_name 2>&1
  24. #
  25. # The actaul timing data is sent to standard error, file
  26. # descriptor (2), and the function name header is sent
  27. # to standard output, file descriptor (1).
  28. #
  29. #######################################################################
  30. #
  31. # set -n # Uncomment to check command syntax without any execution
  32. # set -x # Uncomment to debug this script
  33. #
  34. FILENAME="$1"
  35. DESTFILE="quest1.pl"
  36. TIMEFILE="/tmp/loopfile.out"
  37. >$TIMEFILE
  38. THIS_SCRIPT=$(basename $0)
  39. ######################################
  40. function usage
  41. {
  42. echo "\nUSAGE: $THIS_SCRIPT file_to_process\n"
  43. echo "OR - To send the output to a file use: "
  44. echo "\n$THIS_SCRIPT file_to_process > output_file_name 2>&1 \n"
  45. exit 1
  46. }
  47. ######################################
  48. function while_read_LINE
  49. {
  50. compte=0
  51. cat $FILENAME | while read LINE
  52. do
  53. print_next=$(($print_next-1))
  54. if [[ $print_next -eq 1 ]]; then
  55. code=${LINE##+(rep : )}
  56. echo -n ${code:0:2} >> $DESTFILE
  57. echo -n "," >> $DESTFILE
  58. fi
  59. if [[ $print_next -eq 0 ]]; then
  60. echo -n "[" >> $DESTFILE
  61. if [[ ${#LINE} > 1 ]]; then
  62. echo -n ${LINE:2:1000}|sed 's/ /,/g' >> $DESTFILE
  63. fi
  64. echo -n "]" >> $DESTFILE
  65. echo ")." >> $DESTFILE
  66. fi
  67. if [[ $LINE = quest1* ]];then
  68. compte=$(($compte+1))
  69. print_next=2
  70. echo -n "reponse(" >> $DESTFILE
  71. echo -n "$compte" >> $DESTFILE
  72. echo -n "," >> $DESTFILE
  73. fi
  74. done
  75. }
  76. ######################################
  77. ########### START OF MAIN ############
  78. ######################################
  79. # Test the Input
  80. # Looking for exactly one parameter
  81. (( $# == 1 )) || usage
  82. # Does the file exist as a regular file?
  83. [[ -f $1 ]] || usage
  84. while_read_LINE