Created
August 6, 2011 16:14
-
-
Save narath/1129474 to your computer and use it in GitHub Desktop.
Ruby Easy Grep Script
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/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