File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66
77# to number all lines
88parser .add_argument ("-n" , action = "store_true" , help = "Number all lines" )
9+
10+ # to number non-blank lines
11+ parser .add_argument ("-b" , action = "store_true" , help = "Number non-blank lines (overrides -n)" )
12+
913# To raed files
1014parser .add_argument ("files" , nargs = "+" , help = "Files to read" )
1115
1216args = parser .parse_args ()
1317
18+ if args .n and args .b :
19+ args .n = False
20+
1421line_number = 1
1522
1623for file in args .files :
2128
2229 with open (file , "r" ) as f :
2330 for line in f :
24- if args .n :
31+ if args .b :
32+ if line .strip (): #to number non blank lines only
33+ print (f"{ line_number :6} \t { line } " , end = "" )
34+ line_number += 1
35+ else :
36+ print (line , end = "" ) # to print blank line with number
37+
38+ elif args .n :
2539 print (f"{ line_number :6} \t { line } " , end = "" ) # to print number all lines
2640 line_number += 1
2741 else :
You can’t perform that action at this time.
0 commit comments