#!/bin/bash # copyright 2007 Gilbert Ashley # BashTrix uniq is an implementation of the 'uniq' command # written in pure shell. Very few uniq options are supported. VERSION=0.1 ERROR=0 # show program usage show_usage() { echo "Usage: ${0##/*} [OPTION]... [FILE]..." echo "Try '${0##/*} --help' for more information." exit $ERROR } # show program help show_help() { echo echo "Usage: ${0##/*} [OPTION]... [FILE]..." echo " or: (cat|echo) | ${0##/*} [OPTION]..." echo "Omit duplicate identical lines of each FILE and" echo "print the result to standard output." echo "With no FILE, read standard input." echo " --help display this help and exit" echo " --version output version information and exit" exit $ERROR } show_version() { echo ${0##/*}" (BashTrix) $VERSION" echo "Copyright 2007 Gilbert Ashley " echo "This is free software written in pure shell." exit $ERROR } # get the - options DUMMY for WORD in "$@" ; do case $WORD in -*) true ; case $WORD in --help) show_help ;; --version) show_version ;; -) READ_STDIN=1 ; shift ;; esac ;; esac done if [[ $# -gt 0 ]] ; then while [[ $# -gt 0 ]] ; do FILE_NAME="$1" if [ ! -r "$1" ] ; then echo "Cannot find file $1" 1>&2 exit 1 else # read the lines of the file into an array LINE[$FILE_LINE_COUNT] FILE_LINE_COUNT=0 LINE= IFS=$'\n' while read LINE ; do (( FILE_LINE_COUNT++ )) declare LINE[$FILE_LINE_COUNT]=$LINE done <"$1" fi # now compare each line COUNT=0 while [[ $COUNT -lt $FILE_LINE_COUNT ]] ; do (( COUNT++ )) NEXT_LINE=$COUNT+1 echo ${LINE[$COUNT]} if [[ "${LINE[$COUNT]}" = "${LINE[$NEXT_LINE]}" ]] ; then unset LINE[$NEXT_LINE] #FILE_LINE_COUNT=$(( $FILE_LINE_COUNT - 1 )) #COUNT=$(( $COUNT + 2 )) (( COUNT++ )) fi done # go to next FILE in $@ shift done else # piped input is presumed to be separated into lines already FILE_NAME="STDIN" # read the lines from stdin into an array LINE[$FILE_LINE_COUNT] FILE_LINE_COUNT=0 LINE= IFS=$'\n' while read LINE ; do # add the curent line to the line counter (( FILE_LINE_COUNT++ )) declare LINE[$FILE_LINE_COUNT]=$LINE done # now compare each line COUNT=0 while [[ $COUNT -lt $FILE_LINE_COUNT ]] ; do (( COUNT++ )) NEXT_LINE=$COUNT+1 echo ${LINE[$COUNT]} if [[ "${LINE[$COUNT]}" = "${LINE[$NEXT_LINE]}" ]] ; then unset LINE[$NEXT_LINE] (( COUNT++ )) fi done fi