Skip to content

Instantly share code, notes, and snippets.

@boone
Created December 7, 2008 16:24
Show Gist options
  • Save boone/33175 to your computer and use it in GitHub Desktop.
Save boone/33175 to your computer and use it in GitHub Desktop.
Using grep from Ruby
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment