Skip to content

Instantly share code, notes, and snippets.

@tarVolcano
Created February 26, 2014 13:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tarVolcano/9229735 to your computer and use it in GitHub Desktop.
対象ファイルを検索して、不足しているキーを表示する
#!ruby -Ku
require 'csv'
require 'kconv'
class String #Stringクラスにメソッド追加(表示色)
def c(ccode)
"\e[#{ccode}m#{self}\e[0m"
end
def red; c(31); end
def cyan; c(36); end
end
def search(seekArray, key)
seekArray.each {|line|
if /#{key}/ =~ line #キーにマッチする行を返す、なければnilを返す
return line
end
}
return nil
end
def seek2(keyCSV, seekFile)
keyArray = []
seekArray = []
CSV.open(keyCSV, 'r:sjis') {|csv| keyArray = csv.readlines}
File.open(seekFile,'r:sjis') {|txt| seekArray = txt.readlines}
keyArray.each {|keyLine|
msg = msg2 = ''
if search(seekArray, '"' + keyLine[0] + '"') == nil
msg = keyLine[0] + ' [' + keyLine[2].toutf8 + ']'
if keyLine[1] == 'i' #必須キー(important)が見つからない=>赤字で表示
print msg.red
else
print msg
end
if keyLine[3] == nil
msg2 = ' '
else #見つからなかったキーに、関連キーが設定されていればそれを検索
foundText = search(seekArray, '"' + keyLine[3] + '"')
parseText = foundText == nil ? [''] : foundText.parse_csv
{'KI' => 4, 'OP' => 10, 'S' => 2, '' => 0}.each {|k, i| #区分に応じて取得列を決定
if /^#{k}/ =~ parseText[0]
msg2 = ' : ' + parseText[i].toutf8 #関連キーの情報を併せて表示
break
end
}
end
puts msg2
end
}
end
#メイン処理
csvArray = ['./keyList.csv'] #キーを列挙したCSVファイル、配列で複数ファイル指定可
seekFile = './seekFile.txt' #検索対象ファイル
puts 'Non-existent keys are shown below:'.cyan
csvArray.each {|csvFile|
seek2(csvFile, seekFile) #対象ファイルを検索して、不足しているキーを表示する
}
#対象ファイル内で、キーが重複した行があれば表示する
File.open(seekFile,'r:sjis') {|txt|
buf = ''; isDuplicated = false
txt.each {|line|
if line.parse_csv[1] == buf
isDuplicated = true
break
else
buf = line.parse_csv[1]
end
}
if isDuplicated
puts 'Duplicated key was found. : ' + buf.red
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment