boone (owner)

Revisions

gist: 33175 Download_button fork
public
Description:
Using grep from Ruby
Public Clone URL: git://gist.github.com/33175.git
find_num.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# original slow method to find a number plus a divider (|)
# at the start of a line
def find_num(file, num)
  found = false
  File.read(file).each do |line|
  if line.chomp.split('|', -1)[0] == num
    found = true
    break
  end
end
 
# new method that uses the system's grep
# since this calls the system, make sure that you trust the values
# of file and num
def find_num(file, num)
  found = false
  found = true if %x[grep -cm1 "^#{num}|" "#{file}"].chomp == '1'
  found
end