Module: Sed
- Defined in:
- lib/bugzyrb/common/sed.rb
Overview
changes one or more rows based on pattern and replacement Also if replacement is not given, expects a block and yields matching lines to it
Instance Method Summary collapse
-
#_read(filename) ⇒ Object
read the given filename into an array.
-
#_write(filename, array) ⇒ Object
write the given array to the filename.
- #change_file(filename) ⇒ Object
- #change_row(filename, pattern, replacement = nil) ⇒ Object
-
#delete_row(filename, pattern = nil) ⇒ Object
deletes on more rows based on a pattern Also takes a block and yields each row to it.
-
#egrep(filelist, pattern, egrepoptions = {}) ⇒ Array?
searches filelist array for pattern yielding filename, linenumber and line Taken from facets.rubyforge.org/apidoc/index.html more filelist.
-
#insert_row(filename, lineno, text) ⇒ Object
inserts text in filename at lineno.
Instance Method Details
#_read(filename) ⇒ Object
read the given filename into an array
60 61 62 63 64 65 66 |
# File 'lib/bugzyrb/common/sed.rb', line 60 def _read filename d = [] File.open(filename).each { |line| d << line } return d end |
#_write(filename, array) ⇒ Object
write the given array to the filename
69 70 71 72 73 |
# File 'lib/bugzyrb/common/sed.rb', line 69 def _write filename, array File.open(filename, "w") do |file| array.each { |row| file.puts row } end end |
#change_file(filename) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/bugzyrb/common/sed.rb', line 30 def change_file filename d = _read filename d.each { |row| yield row } _write filename, d end |
#change_row(filename, pattern, replacement = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bugzyrb/common/sed.rb', line 17 def change_row filename, pattern, replacement = nil d = _read filename d.each { |row| if row =~ pattern if replacement row.gsub!( pattern, replacement) else yield row end end } _write filename, d end |
#delete_row(filename, pattern = nil) ⇒ Object
deletes on more rows based on a pattern Also takes a block and yields each row to it
40 41 42 43 44 45 46 47 48 |
# File 'lib/bugzyrb/common/sed.rb', line 40 def delete_row filename, pattern = nil d = _read filename if pattern d.delete_if { |row| row =~ pattern } else d.delete_if { |row| yield row } end _write filename, d end |
#egrep(filelist, pattern, egrepoptions = {}) ⇒ Array?
searches filelist array for pattern yielding filename, linenumber and line Taken from facets.rubyforge.org/apidoc/index.html more filelist. egrepoptions - count only, linenumber, filename, invert etc
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bugzyrb/common/sed.rb', line 80 def egrep(filelist, pattern, ={}) lines = [] filelist.each do |fn| open(fn) do |inf| count = 0 inf.each do |line| count += 1 if pattern.match(line) if block_given? yield fn, count, line else #puts "#{fn}:#{count}:#{line}" lines << "#{fn}#{@todo_delim}#{count}#{@todo_delim}#{line}" end end end end end unless block_given? ret = lines.empty? nil:lines return ret end end |