Last active
August 29, 2015 14:13
-
-
Save pablotron/d934b8d760b0cb0074df to your computer and use it in GitHub Desktop.
fys
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
pabs@halcyon:~/git/fys> perl perl.pl | |
host,time | |
blargh.org,"60d 3h" | |
blum.net,"40d 3h" | |
baz.biz,"32d 3h" |
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/perl | |
use warnings; | |
use strict; | |
use Text::CSV; | |
# create csv parser | |
my $csv = Text::CSV->new({ | |
binary => 1 | |
}) or die "Cannot create CSV parser: " . Text::CSV->error_diag(); | |
# read csv rows | |
my @rows; | |
# load input file | |
open my $fh, '<:encoding(utf8)', 'test.csv' | |
or die "Couldn't open test.csv: $!"; | |
# read rows | |
while (my $row = $csv->getline($fh)) { | |
# skip rows that do not have a status of "down" | |
$row->[1] =~ /down/ or next; | |
# get number of days | |
my $days = ($row->[2] =~ /(\d+)d/) ? $1 : 0; | |
# skip rows that are less than 30 days old | |
next unless $days > 30; | |
# add to output | |
push @rows, { | |
host => $row->[0], | |
time => $row->[2], | |
days => $days, | |
}; | |
} | |
# set line terminator | |
$csv->eol("\n"); | |
# print csv header | |
$csv->print(*STDOUT, ['host', 'time']); | |
# write rows to output | |
$csv->print(*STDOUT, [$_->{host}, $_->{time}]) for (sort { | |
# sort rows in reverse order | |
$b->{days} <=> $a->{days} | |
} @rows); |
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
pabs@halcyon:~/git/fys> python python.py | |
host,time | |
blargh.org,60d 3h | |
blum.net,40d 3h | |
baz.biz,32d 3h |
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/python | |
# load libraries | |
import re, csv, sys | |
# cache down regex | |
DOWN_RE = re.compile(r"down", re.I) | |
def is_down(val): | |
return re.match(DOWN_RE, val) | |
# cache days regex | |
DAYS_RE = re.compile(r"(\d+)d") | |
def to_days(val): | |
# match days portion of time value | |
md = re.match(DAYS_RE, val) | |
# return 0 if regex didn't match | |
if md is None: | |
return 0 | |
# convert number of days to integer | |
return int(md.group(1)) | |
# open output csv and write header | |
out = csv.writer(sys.stdout) | |
out.writerow(["host", "time"]) | |
# open input csv "test.csv" | |
with open('test.csv', 'rb') as src_fh: | |
# convert to rows and filter on relevant rows | |
rows = filter(lambda row: row["down"] and row["days"] > 30, [{ | |
"host": row[0], | |
"down": is_down(row[1]), | |
"time": row[2], | |
"days": to_days(row[2]), | |
} for row in csv.reader(src_fh)]) | |
# sort rows by descending number of days | |
rows.sort(lambda a, b: cmp(b["days"], a["days"])) | |
# print rows to output csv | |
for row in rows: | |
out.writerow([row["host"], row["time"]]) |
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
pabs@halcyon:~/git/fys> ruby ruby.rb | |
host,time | |
blargh.org,60d 3h | |
blum.net,40d 3h | |
baz.biz,32d 3h |
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 | |
# load csv library | |
require 'csv' | |
# write to standard output | |
CSV($stdout) do |out| | |
# write output csv header | |
out << %w{host time} | |
# read from input csv "test.csv" | |
CSV.read('test.csv').map do |row| | |
# match days portion of time column | |
md = row[2].match(/(\d+)d/) | |
# convert to integer, or zero if we couldn't match any day value | |
days = md ? md[1].to_i : 0 | |
# map source row to hash of values | |
{ | |
host: row[0], | |
down: row[1] =~ /down/, | |
time: row[2], | |
days: days, | |
} | |
end.select do |row| | |
# filter on rows that we care about | |
row[:down] && row[:days] > 30 | |
end.sort do |a, b| | |
# sort rows in reverse order | |
b[:days] <=> a[:days] | |
end.each do |row| | |
# convert to csv row and add to output | |
out << [row[:host], row[:time]] | |
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
host | status | time | |
---|---|---|---|
foo.com | up | 0d 4h | |
bar.com | down | 15d 3h | |
baz.biz | down | 32d 3h | |
blum.net | down | 40d 3h | |
blargh.org | down | 60d 3h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment