/ Published in: Python
unwraps lines from a text file with parameters
Expand |
Embed | Plain Text
#!/usr/bin/python import sys import getopt def usage(): print "Usage: %(filename)s -n 4" % { "filename" : sys.argv[0] } print " unwraps 4 lines of output to a single line" def main(argv=None): if argv is None: argv = sys.argv try: opts, args = getopt.getopt(argv[1:], "n:", ["number-of-lines"]) num_lines = 0 for o, a in opts: if o == "-n": num_lines = int(a) assert (num_lines > 0) i = 0 buff = "" for line in sys.stdin: buff = buff + " " + line.lstrip().rstrip() i = i + 1 if i >= num_lines: print buff i = 0 buff = "" if i != 0: print buff except getopt.GetoptError, err: print str(err) usage() return 2; return 0; if __name__ == "__main__": sys.exit(main())
You need to login to post a comment.
