0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Linuxのuniqコマンドに相当するコードをPythonで実装してみた

Posted at

Linuxのuniqコマンドに相当するコードをPythonで実装してみました。オプションは-c、-u、-d実装しました。

.
├── uniq.py
├── test.txt
uniq.py
import sys


def uniq(f):
    previous_line = ""
    for line in f.readlines():
        if previous_line == line:
            continue

        print(line, end="")
        previous_line = line


def uniq_c(f):
    previous_line = ""
    count = 1
    for line in f.readlines():
        if previous_line == "":
            previous_line = line
            continue

        if previous_line == line:
            count += 1
            continue

        print("{} {}".format(count, previous_line), end="")
        previous_line = line
        count = 1

    print("{} {}".format(count, previous_line), end="")


def uniq_u(f):
    previous_line = ""
    is_unique = True
    for line in f.readlines():
        if previous_line == "":
            previous_line = line
            continue

        if previous_line == line:
            is_unique = False
            continue

        if is_unique:
            print(previous_line, end="")

        previous_line = line
        is_unique = True

    if is_unique:
        print(previous_line, end="")


def uniq_d(f):
    previous_line = ""
    is_repeated = False
    for line in f.readlines():
        if previous_line == "":
            previous_line = line
            continue

        if previous_line == line:
            is_repeated = True
            continue

        if is_repeated:
            print(previous_line, end="")

        previous_line = line
        is_repeated = False

    if is_repeated:
        print(previous_line, end="")


func_dictionary = {"-c": uniq_c, "-u": uniq_u, "-d": uniq_d}
try:
    if len(sys.argv) > 1:
        f = open(sys.argv[-1], "r")
        if len(sys.argv) == 2:
            uniq(f)
        elif len(sys.argv) == 3:
            if func_dictionary.get(sys.argv[1]):
                func_dictionary.get(sys.argv[1])(f)
            else:
                print("args:[-c|-u|-d]")
    else:
        f = sys.stdin
        uniq(f)
except FileNotFoundError:
    print('Error: File {0} is not found.'.format(sys.argv[1]), file=sys.stderr)
    sys.exit(1)
test.txt
aaa
bbb
aaa
aaa
AAA

以下が出力結果です。

$ python uniq.py test.txt
aaa
bbb
aaa
AAA
$ python uniq.py -c test.txt
1 aaa
1 bbb
2 aaa
1 AAA
$ python uniq.py -u test.txt
aaa
bbb
AAA
$ python uniq.py -d test.txt
aaa
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?