Created
December 7, 2008 16:24
-
-
Save boone/33175 to your computer and use it in GitHub Desktop.
Using grep from Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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