Module: Sed

Defined in:
lib/common/sed.rb

Overview

!/usr/bin/env ruby -w ************************************************************* Sed like operations - just change row and delete row While converting some shell scripts in which i have used sed extensively, I need this kind of functionality

*************************************************************

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)

Instance Method Details

- (Object) _read(filename)

read the given filename into an array



57
58
59
60
61
62
63
# File 'lib/common/sed.rb', line 57

def _read filename
  d = []
  File.open(filename).each { |line|
    d << line
  }
  return d
end

- (Object) _write(filename, array)

write the given array to the filename



66
67
68
69
70
# File 'lib/common/sed.rb', line 66

def _write filename, array
  File.open(filename, "w") do |file| 
    array.each { |row| file.puts row }
  end
end

- (Object) change_file(filename)



27
28
29
30
31
32
33
# File 'lib/common/sed.rb', line 27

def change_file filename
  d = _read filename
  d.each { |row|
    yield row
  }
  _write filename, d
end

- (Object) change_row(filename, pattern, replacement = nil)



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/common/sed.rb', line 14

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

- (Object) delete_row(filename, pattern = nil)

deletes on more rows based on a pattern Also takes a block and yields each row to it



37
38
39
40
41
42
43
44
45
# File 'lib/common/sed.rb', line 37

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

- (Array?) egrep(filelist, pattern, egrepoptions = {})

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

Returns:

  • (Array, nil)

    array of lines containing filename,lineno,line tab delimited or nil if nothing found



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/common/sed.rb', line 77

def egrep(filelist, pattern, egrepoptions={})
  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

- (Object) insert_row(filename, lineno, text)

inserts text in filename at lineno.



49
50
51
52
53
54
# File 'lib/common/sed.rb', line 49

def insert_row filename, lineno, text
  d = _read filename
  d.insert(lineno, text)
  _write filename, d
  0
end