Created
January 20, 2011 00:26
-
-
Save sflinter/787178 to your computer and use it in GitHub Desktop.
A ruby clone of ack (http://betterthangrep.com/) that uses Ruby multiline regexps.
This file contains 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
#!/usr/bin/env ruby | |
require 'find' | |
usage = <<EOF | |
Usage: ack.rb <regexp> <directory or filename> | |
The regular expression can be across multiple lines, and is _not_ escaped by default. | |
For example: | |
ack.rb 'end\\nend' . | |
will find all files in the current directory that have two 'end' statements across two lines | |
ack.rb is inspired by (but much less featureful than) ack (http://betterthangrep.com/) | |
EOF | |
abort usage unless ARGV.size == 2 | |
regexp = Regexp.new(ARGV[0]) | |
puts "Using regexp: #{regexp}" | |
filename = ARGV[1] | |
Find.find(filename) do |file| | |
next if File.directory? file | |
begin | |
File.open(file) do |f| | |
f.read(nil, s = "") | |
i = regexp =~ s | |
if i | |
puts "*** File: #{file}" | |
puts s[i-200..i+200] | |
end | |
end | |
rescue | |
puts "Cannot open file #{file}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment