Last active
September 23, 2015 06:08
-
-
Save sizer/87b6d43be2eb0d55aa95 to your computer and use it in GitHub Desktop.
Read and grep csv by ruby
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
FILE_NAME = ARGV[0] | |
KEYWORD = ARGV[1] | |
SPLITTER = /,/ | |
def howto | |
puts "Howto? " + File.basename($0) + " <filename>" + " <keyword>" | |
end | |
# 引数が想定しないパターンの場合にエラー表示。 | |
if ARGV.empty? || ARGV.count() != 2 | |
howto() | |
end | |
# ファイルが存在する場合、各行を要素ごとに分割した後、キーワードを検索。 | |
# キーワードに一致する要素が存在する場合、標準出力を行う。 | |
if File.exist?(FILE_NAME) | |
count = 0 | |
File.readlines(FILE_NAME).each do |line| | |
line.split(SPLITTER).each do |word| | |
word.chomp! | |
if word.include?(KEYWORD) | |
count += 1 | |
puts line | |
end | |
end | |
end | |
puts count.to_s + "lines in " + FILE_NAME + " \"" + KEYWORD + "\"" | |
else | |
puts FILE_NAME + " file not found." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
update last column cannot match