/0.1/Algebre/script-quest1-pl
Korn Shell | 91 lines | 43 code | 7 blank | 41 comment | 6 complexity | e4825ec08fe81ee3ef191f8469431d47 MD5 | raw file
- #!/usr/bin/ksh
- #
- # SCRIPT: 12_ways_to_parse.ksh.ksh
- #
- #
- # REV: 1.2.A
- #
- # PURPOSE: This script shows the different ways of reading
- # a file line by line. Again there is not just one way
- # to read a file line by line and some are faster than
- # others and some are more intuitive than others.
- #
- # REV LIST:
- #
- # 03/15/2002 - Randy Michael
- # Set each of the while loops up as functions and the timing
- # of each function to see which one is the fastest.
- #
- #######################################################################
- #
- # NOTE: To output the timing to a file use the following syntax:
- #
- # 12_ways_to_parse.ksh file_to_process > output_file_name 2>&1
- #
- # The actaul timing data is sent to standard error, file
- # descriptor (2), and the function name header is sent
- # to standard output, file descriptor (1).
- #
- #######################################################################
- #
- # set -n # Uncomment to check command syntax without any execution
- # set -x # Uncomment to debug this script
- #
- FILENAME="$1"
- DESTFILE="quest1.pl"
- TIMEFILE="/tmp/loopfile.out"
- >$TIMEFILE
- THIS_SCRIPT=$(basename $0)
- ######################################
- function usage
- {
- echo "\nUSAGE: $THIS_SCRIPT file_to_process\n"
- echo "OR - To send the output to a file use: "
- echo "\n$THIS_SCRIPT file_to_process > output_file_name 2>&1 \n"
- exit 1
- }
- ######################################
- function while_read_LINE
- {
- compte=0
- cat $FILENAME | while read LINE
- do
- print_next=$(($print_next-1))
- if [[ $print_next -eq 1 ]]; then
- code=${LINE##+(rep : )}
- echo -n ${code:0:2} >> $DESTFILE
- echo -n "," >> $DESTFILE
- fi
- if [[ $print_next -eq 0 ]]; then
- echo -n "[" >> $DESTFILE
- if [[ ${#LINE} > 1 ]]; then
- echo -n ${LINE:2:1000}|sed 's/ /,/g' >> $DESTFILE
- fi
- echo -n "]" >> $DESTFILE
- echo ")." >> $DESTFILE
- fi
- if [[ $LINE = quest1* ]];then
- compte=$(($compte+1))
- print_next=2
- echo -n "reponse(" >> $DESTFILE
- echo -n "$compte" >> $DESTFILE
- echo -n "," >> $DESTFILE
- fi
- done
- }
- ######################################
- ########### START OF MAIN ############
- ######################################
- # Test the Input
- # Looking for exactly one parameter
- (( $# == 1 )) || usage
- # Does the file exist as a regular file?
- [[ -f $1 ]] || usage
- while_read_LINE