|
| 1 | +class FileEdit |
| 2 | + |
| 3 | + require 'ftools' |
| 4 | + |
| 5 | + private |
| 6 | + attr_accessor :filename, :contents, :match |
| 7 | + |
| 8 | + public |
| 9 | + |
| 10 | + def initialize(filepath) |
| 11 | + @filename = filepath |
| 12 | + @contents = Array.new |
| 13 | + @match = false |
| 14 | + |
| 15 | + |
| 16 | + raise ArgumentError, "File doesn't exist" unless (ret = File.exist? @filename) |
| 17 | + |
| 18 | + f = File.new(@filename) |
| 19 | + raise ArgumentError, "File is blank" unless (@contents = f.readlines).length > 0 |
| 20 | + |
| 21 | + end |
| 22 | + |
| 23 | + |
| 24 | + #search the file line by line and match each line with the given regex |
| 25 | + #if matched, replace the whole line with newline. |
| 26 | + def search_file_replace_line(regex, newline) |
| 27 | + search_match(regex, newline, 'r', 1) |
| 28 | + end |
| 29 | + |
| 30 | + #search the file line by line and match each line with the given regex |
| 31 | + #if matched, replace the match (all occurances) with the replace parameter |
| 32 | + def search_file_replace(regex, replace) |
| 33 | + search_match(regex, replace, 'r', 2) |
| 34 | + end |
| 35 | + |
| 36 | + #search the file line by line and match each line with the given regex |
| 37 | + #if matched, delete the line |
| 38 | + def search_file_delete_line(regex) |
| 39 | + search_match(regex, " ", 'd', 1) |
| 40 | + end |
| 41 | + |
| 42 | + #search the file line by line and match each line with the given regex |
| 43 | + #if matched, delete the match (all occurances) from the line |
| 44 | + def search_file_delete(regex) |
| 45 | + search_match(regex, " ", 'd', 2) |
| 46 | + end |
| 47 | + |
| 48 | + #search the file line by line and match each line with the given regex |
| 49 | + #if matched, insert newline after each matching line |
| 50 | + def insert_line_after_match(regex, newline) |
| 51 | + search_match(regex, newline, 'i', 0) |
| 52 | + end |
| 53 | + |
| 54 | + #Make a copy of old_file and write new file out (only if file changed) |
| 55 | + def write_file |
| 56 | + # @match is false when there is no match in the whole file. Nothing need to be done. |
| 57 | + if @match == true |
| 58 | + File.copy(@filename, @filename + ".old") |
| 59 | + newfile = File.new("temp", "w") |
| 60 | + @contents.each() do |line| |
| 61 | + newfile.puts(line) |
| 62 | + end |
| 63 | + newfile.close |
| 64 | + File.rename("temp", @filename) |
| 65 | + end |
| 66 | + @match = false |
| 67 | + end |
| 68 | + |
| 69 | + #helper method to search_file_replace_delete_line, search_file_replace_delete, and insert_line_after_match |
| 70 | + def search_match(regex, replace, command, method) |
| 71 | + |
| 72 | + raise ArgumentError, "command should be 'r' or 'd'" unless (command == 'r' or command == 'd' or command == 'i') |
| 73 | + |
| 74 | + #check if regex is Regexp object or simple string and store the Regexp object in exp. |
| 75 | + (regex.kind_of? Regexp)? exp = regex : exp = Regexp.new(regex) |
| 76 | + |
| 77 | + i = 0 |
| 78 | + begin |
| 79 | + line = @contents[i] |
| 80 | + if line =~ exp |
| 81 | + @match = true |
| 82 | + case |
| 83 | + when command == 'r' |
| 84 | + (method == 1)? @contents[i] = replace: @contents[i].gsub!(exp, replace) |
| 85 | + when command == 'd' |
| 86 | + if method == 1 |
| 87 | + @contents.delete_at(i) |
| 88 | + i = i - 1 |
| 89 | + else |
| 90 | + @contents[i].gsub!(exp, "") |
| 91 | + end |
| 92 | + when command == 'i' |
| 93 | + @contents.insert(i+1, replace) |
| 94 | + end |
| 95 | + end |
| 96 | + i = i+1 |
| 97 | + end until i == @contents.length |
| 98 | + end |
| 99 | + |
| 100 | + |
| 101 | +end |
| 102 | + |
| 103 | +#test |
| 104 | +if __FILE__ == $0 |
| 105 | + fedit = FileEdit.new("test") |
| 106 | + |
| 107 | + fedit.insert_line_after_match(/test/, "new Line Inserted") |
| 108 | + fedit.search_file_replace(/test/, "replace") |
| 109 | + fedit.search_file_replace_line(/replace/, "this line is replaced") |
| 110 | + fedit.search_file_delete(/this/) |
| 111 | + fedit.search_file_delete_line(/new/) |
| 112 | + fedit.write_file() |
| 113 | +end |
0 commit comments