Program uniq

From Software Engineers Wiki
Jump to: navigation, search

Implement uniq program that omits repeated lines.

Answer #1 python implementation

import sys, getopt

def usage():
        print("uniq.py [OPTIONS] <filename>")
        print("  -c, --count: prefix lines by the number of occurrences")
        print("  -d, --repeated: only print duplicate lines")
        print("  -i, --ignore-case: ignore differences in case when comparing")
        print("  -u, --unique: only print unique lines")

def strCompare(str1, str2, ignoreCase):
        if ignoreCase:
                return str1.lower() == str2.lower()
        else:
                return str1 == str2

def printLineWithCount(str1, printCount, count):
        if printCount:
                sys.stdout.write(str(count) + ": ")
        sys.stdout.write(str1)

def main():
        # parse parameters
        paramCount = False
        paramRepeated = False
        paramIgnoreCase = False
        paramUnique = False
        paramFileName = None

        try:
                opts, args = getopt.getopt(sys.argv[1:], "cdiu", [ "count", "repeated", "ignore-case", "unique" ])

                if (len(args) == 0):
                        usage()
                        sys.exit(1)
                paramFileName = args[0]

                for opt, optArg in opts:
                        if opt in ("-c", "--count"):
                                paramCount = True
                        if opt in ("-d", "--repeated"):
                                paramRepeated = True
                        if opt in ("-i", "--ignore-case"):
                                paramIgnoreCase = True
                        if opt in ("-u", "--unique"):
                                paramUnique = True

        except getopt.GetoptError as e:
                print(str(e))
                sys.exit(1)

        f = None
        try:
                f = open(paramFileName, "r")
        except IOError as e:
                print(str(e))
                sys.exit(1)
        except OSError as e:
                print(str(e))
                sys.exit(1)

        lastLine = ""
        lastLineCount = 0
        curLine = ""
        doExit = False

        while True:
                curLine = f.readline()
                if len(curLine) == 0:
                        doExit = True

                if lastLine == "":
                        lastLine = curLine[:]
                        lastLineCount = 1
                else:
                        if strCompare(lastLine, curLine, paramIgnoreCase):
                                # same as previous line
                                lastLineCount += 1
                        else:
                                # different from previous line
                                doPrint = False
                                if paramUnique:
                                        if lastLineCount == 1:
                                                doPrint = True
                                elif paramRepeated:
                                        if lastLineCount > 1:
                                                doPrint = True
                                else:
                                        doPrint = True

                                if doPrint:
                                        printLineWithCount(lastLine, paramCount, lastLineCount)

                                lastLine = curLine[:]
                                lastLineCount = 1

                if doExit:
                        break

        f.close()

main()
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox