1 #! /usr/bin/env python 2 # Time-stamp: <2001-10-04 10:06:58 cymbala> 3 # 4 # What's in a line? 5 # ----------------------------------------------------------------------------- 6 # This counts numbers of linefeeds (LF) and carriage returns (CR) per file. 7 # c nt l f c r 8 # cnt_lfcr.py 9 # 10 # Example usage: 11 # python ~/bin/cnt_lfcr.py `find archive/lenin/works/ -name '*.htm' | egrep '1895/misc'` 12 # 13 # Example output: 14 # archive/lenin/works/1895/misc/holy-fam/holy-fam.htm 1494 0 15 # archive/lenin/works/1895/misc/holy-fam/holy-s2.htm 683 683 16 # archive/lenin/works/1895/misc/engel-bio.htm 0 374 17 # 18 # ----------------------------------------------------------------------------- 19 20 import sys 21 for file in sys.argv[1:]: 22 counts = {} 23 counts['control_j'] = 0 # linefeeds (LF) 24 counts['control_m'] = 0 # carriage returns (CR) 25 f = open(file) 26 while 't': 27 character = f.read(1) 28 if character == '': 29 break 30 else: 31 if character == '\n': 32 counts['control_j'] = counts['control_j'] + 1 33 pass 34 elif character == '\r': 35 counts['control_m'] = counts['control_m'] + 1 36 pass 37 pass 38 pass 39 f.close() 40 print file, counts['control_j'], counts['control_m'] 41 pass 42 ### 43 # |