Skip to content

Instantly share code, notes, and snippets.

@narath
Created August 6, 2011 16:14
Show Gist options
  • Save narath/1129474 to your computer and use it in GitHub Desktop.
Save narath/1129474 to your computer and use it in GitHub Desktop.
Ruby Easy Grep Script
#!/usr/bin/ruby
# from http://www.3till7.net/2010/02/23/convenient-file-searching-with-ruby-grep-and-file/
unless ARGV.length >= 2
puts "Usage: #$0 file_extension query"
puts "\tExample: #$0 '*.h' 'struct list_head'"
exit
end
unless ARGV.length == 2
extra_args = ARGV[2...ARGV.length].join ', '
puts "Warning: extra arguments ignored: " << extra_args
end
file_extension = ARGV[0]
query = ARGV[1]
command = "find . -type f -name '#{file_extension}' -print0 | xargs -0 grep --line-number --color -H -o '#{query}'"
system 'clear'
puts "Searching #{file_extension} for \"#{query}\"..."
system command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment