|
| 1 | +import re |
| 2 | +import sys |
| 3 | + |
| 4 | +def substitution(file_name, option, what_to_sub, what_to_sub_with, operate_on = "g"): |
| 5 | + print(f"file_name={file_name}, option={option}, what_to_sub={what_to_sub}, what_to_sub_with={what_to_sub_with}, operate_on={operate_on}") |
| 6 | + with open(file_name, "r+") as f: |
| 7 | + file = f.read() |
| 8 | + |
| 9 | + file_modified = re.sub(what_to_sub, what_to_sub_with, file) |
| 10 | + f.seek(0, 0) |
| 11 | + f.write(file_modified) |
| 12 | + f.truncate() |
| 13 | + |
| 14 | +def sedprint(option, start_from, end_to, file_name): |
| 15 | + print(f"option={option}, start_from={start_from}, end_to={end_to}, file_name={file_name}") |
| 16 | + file = open(file_name, "r") |
| 17 | + file_content = file.readlines() |
| 18 | + for i in range(int(start_from)-1, int(end_to)): |
| 19 | + print(file_content[i].strip()) |
| 20 | + |
| 21 | + file.close() |
| 22 | + |
| 23 | +def sedpatternprint(option, string_to_search, what_to_do, file_name): |
| 24 | + print(f"option={option}, string_to_search={string_to_search}, what_to_do={what_to_do}, file_name={file_name}") |
| 25 | + file = open(file_name, "r") |
| 26 | + file_content = file.readlines() |
| 27 | + for line in file_content: |
| 28 | + #print(line) |
| 29 | + if string_to_search in line: |
| 30 | + print(line) |
| 31 | + file.close() |
| 32 | + |
| 33 | +def seddoublespace(option, file_name): |
| 34 | + file = open(file_name) |
| 35 | + file_content = file.readlines() |
| 36 | + file.close() |
| 37 | + with open(file_name, "w") as f: |
| 38 | + for line in file_content: |
| 39 | + f.write(line+"\n\n") |
| 40 | + |
| 41 | +def seddel(option, filename): |
| 42 | + print("hello") |
| 43 | + option_list = option.split("/") |
| 44 | + with open(filename, "r") as f_read: |
| 45 | + data = f_read.read().rstrip("\n") |
| 46 | + with open(filename, "w") as f_write: |
| 47 | + f_write.write(data[:-1]) |
| 48 | + |
| 49 | +################################ Script starts from here ################################ |
| 50 | +## It uses alias ccsed="python ccsed.py" |
| 51 | +## take this into consideration when accepting command line arguments |
| 52 | + |
| 53 | +if sys.argv[1] == "-n": |
| 54 | + options = sys.argv[2] |
| 55 | + #print(sys.argv[1], sys.argv[2]) |
| 56 | + try: #ccsed -n '2,4p' file_name |
| 57 | + options_list = options.split(",") |
| 58 | + sedprint(sys.argv[1], options_list[0],options_list[1][0], sys.argv[3]) |
| 59 | + except: #ccsed -n /roads/p unquoted.txt |
| 60 | + options_list = options.split("/") |
| 61 | + print(options_list) |
| 62 | + sedpatternprint(sys.argv[1], options_list[1],options_list[2], sys.argv[3]) |
| 63 | +elif sys.argv[1].upper() == "G": |
| 64 | + file_name = sys.argv[2] |
| 65 | + seddoublespace(sys.argv[1], file_name) |
| 66 | + |
| 67 | +elif sys.argv[1].split("/")[1] == "^$": |
| 68 | + seddel(sys.argv[1], sys.argv[2]) |
| 69 | + |
| 70 | +else: #ccsed s/this/that/g file_name |
| 71 | + options = sys.argv[1] |
| 72 | + file_name = sys.argv[2] |
| 73 | + options_list = options.split("/") |
| 74 | + if options_list[0].lower() == "s": |
| 75 | + try: |
| 76 | + if options_list[3].lower() != " ": |
| 77 | + substitution(file_name, "s", options_list[1], options_list[2], options_list[3].lower()) |
| 78 | + except IndexError: |
| 79 | + substitution(file_name, "s", options_list[1], options_list[2]) |
0 commit comments