Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python equivalent of GNU 'cat' that shows unique lines

Tags:

python

Has anyone written the GNU cat command in python and would be willing to share? GNU cat actually does quite a bit & I don't really feel like re-inventing the wheel today. Yes, I did do a google search & and after reading too many sad stories of kittens vs snakes I decided to try SO.

Edit: I'd like to modify it so that it only shows unique lines.

like image 478
JohnnyLambada Avatar asked Dec 13 '25 12:12

JohnnyLambada


1 Answers

Latest: Thank's Ned for the fileinput tip! Here's the latest:

#!/usr/bin/python

"""cat the file, but only the unique lines
"""
import fileinput

if __name__ == "__main__":
    lines=set()
    for line in fileinput.input():
        if not line in lines:
            print line,
            lines.add(line)

Previously (2010-02-09):

Here's what I ended up with. It works for my immediate need. Thanks Mike Graham for your help.

#!/usr/bin/python
"""cat the file, but only the unique lines
"""
import optparse
import sys

if __name__ == "__main__":
    parser = optparse.OptionParser()
    parser.set_usage('%prog [OPTIONS]')
    parser.set_description('cat a file, only unique lines')

    (options,args) = parser.parse_args()

    lines = set()
    for file in args:
        if file == "-":
            f = sys.stdin
        else:
            f = open(file)
        for line in f:
            if not line in lines:
                print line,
                lines.add(line)
like image 183
JohnnyLambada Avatar answered Dec 16 '25 09:12

JohnnyLambada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!