Created
June 26, 2012 01:32
-
-
Save yuuki/2992573 to your computer and use it in GitHub Desktop.
S3 Data file parser
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/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
if __name__ == '__main__': | |
with file(sys.argv[1], 'r') as f: | |
sum = [0, 0, 0] | |
num = [0, 0, 0] | |
mean = [0, 0, 0] | |
src_ip = None | |
for n, line in enumerate(f): | |
if n is 0: | |
series = line.split(",") | |
print "%s %s %s %s" % (series[0], series[2], series[4], series[6]) | |
continue | |
colums = line.split(" ") | |
if n is 1: | |
src_ip = colums[0] | |
if src_ip != colums[0]: | |
mean = [sum[i]/num[i] if num[i] is not 0 else 0 for i in range(3)] | |
print "%s %s %s %s" % (colums[0], mean[0], mean[1], mean[2]) | |
sum = [0, 0, 0] | |
num = [0, 0, 0] | |
src_ip = colums[0] | |
for i in range(3): | |
value = colums[2*i + 2] | |
if value != "N/A": | |
sum[i] += float(value) | |
num[i] += 1 |
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/env ruby | |
# -*- coding: utf-8 -*- | |
if __FILE__ == $0 | |
(abort "no input file." || exit) unless ARGV[0] | |
sum = [0, 0, 0] | |
num = [0, 0, 0] | |
mean = [0, 0, 0] | |
src_ip = nil | |
File.open(ARGV[0]).each_with_index do |line, n| | |
if n == 0 | |
series = line.split(",") | |
printf "%s %s %s %s\n", series[0], series[2], series[4], series[6] | |
end | |
colums = line.split(" ") | |
if n == 1 | |
src_ip = colums[0] | |
end | |
if src_ip != colums[0] | |
mean = [0, 1, 2].map! { |i| num[i] != 0 ? sum[i] / num[i] : 0 } | |
printf "%s %s %s %s\n", colums[0], mean[0], mean[1], mean[2] | |
sum = [0, 0, 0] | |
num = [0, 0, 0] | |
src_ip = colums[0] | |
end | |
[0, 1, 2].each do |i| | |
value = colums[2 * i + 2] | |
if value != "N/A" | |
sum[i] += value.to_f | |
num[i] += 1 | |
end | |
end | |
end | |
end |
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
#!/bin/bash | |
cat $1 | awk ' | |
BEGIN { | |
for (i = 1; i <= 3; i++) { | |
sum[i] = 0 | |
num[i] = 0 | |
} | |
} | |
NR == 1 { | |
split($0, series, ",") | |
print series[1] " " series[3] " " series[5] " " series[7] | |
next | |
} | |
NR == 2 { | |
src_ip = $1 | |
} | |
$1 != src_ip { | |
for (i = 1; i <= 3; i++) { | |
if (num[i] == 0) { | |
mean[i] = 0 | |
} else { | |
mean[i] = sum[i]/num[i] | |
} | |
} | |
print $1 " " mean[1] " " mean[2] " " mean[3] | |
for (i = 1; i <= 3; i++) { | |
sum[i] = 0 | |
num[i] = 0 | |
} | |
src_ip = $1 | |
} | |
{ | |
if ($3 != "N/A") { | |
sum[1] += $3 | |
num[1] += 1 | |
} | |
if ($5 != "N/A") { | |
sum[2] += $5 | |
num[2] += 1 | |
} | |
if ($7 != "N/A") { | |
sum[3] += $7 | |
num[3] += 1 | |
} | |
next | |
} | |
END { | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment