Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active August 29, 2015 14:11
Show Gist options
  • Save horitaku1124/37b2dc18241a69060ab4 to your computer and use it in GitHub Desktop.
Save horitaku1124/37b2dc18241a69060ab4 to your computer and use it in GitHub Desktop.
httpdのログをq用に整形
require 'date'
file = ARGV[0] == nil ? STDIN : open(ARGV[0])
puts "ip\ta1\ta1\tdate\tmethod\tquery\tprotocol\trespond\tlength\treffer\tua\n"
while line = file.gets
line = line.gsub(/[\r\n ]$/, "")
len = line.length
inQuato = false
inCase = false
values = []
start = 0
index = 0
len.times do |i|
if len <= index then break end
c = line[index]
index += 1
if inQuato
if c == "\""
text = line.slice(start..index-2)
if values.length == 4
attrs = text.split(" ")
if attrs.length == 3
attrs.each do |str|
values << str
end
else
values << text
values << "-"
values << "-"
end
else
values << text
end
start = index
inQuato = false
else
end
elsif inCase
if c == "]"
values << line.slice(start..index-2)
start = index
inCase = false
else
end
else
if c == "\""
inQuato = true
start = index
elsif c == "["
inCase = true
start = index
elsif c == "-"
values << c
index += 1
start = index
elsif c == " "
if start <= index-2
values << line.slice(start..index-2)
end
start = index
else
end
end
end
puts values.join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment