Skip to content

Instantly share code, notes, and snippets.

View nad2000's full-sized avatar
🇳🇿
Working from New Zealand

Radomirs Cirskis nad2000

🇳🇿
Working from New Zealand
View GitHub Profile
@nad2000
nad2000 / monitor_size.sh
Created March 19, 2014 03:49
monitor and analyse file system size change
# Collect data until ^C:
while true ; do df /data ; sleep 1 ; done | tee storage.txt
# Extract "Used field":
grep /dev/md0 storage.txt | sed 's/ */\t/g' | cut -f4
@nad2000
nad2000 / choice_web_migr.sh
Last active August 29, 2015 13:56
choice_web DB transfer form PostgreSQL 8.1 to 9.3
# 1. download and install PostgreSQL 9.3 (remember the password your entered)
# 2. install PostGIS using Stackbuilder (it's a part of PostgreSQL 9.3 installation)
# Make sure postgeres is started
# Run script that fix issues with choice_web:
psql -U postgres -d postgres -f choice_web_migr.sql
# Load the dump (that might take upto 10min):
psql -U postgres -f choice_web_02_21_2014.dump -d choice_web 2>choice_web_log.err
@nad2000
nad2000 / insert_at_the_beginning.sh
Created February 6, 2014 23:35
How do I add text to the beginning of a file in Bash?
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt
# OR
cat <(echo "task goes here") todo.txt > todo_new.txt
# OR
echo -e "task goes here\n$(cat todo.txt)" > todo.txt
# OR
sed -i '1s/^/task goes here\n/' todo.txt
# OR
sed -i '1itask goes here' todo.txt
@nad2000
nad2000 / fib.py
Last active March 31, 2018 20:06
Threading with Python
def fib(n):
if n <= 2:
return 1
else:
return fib(n-1) + fib(n-2)
@nad2000
nad2000 / setup_git.sh
Last active January 1, 2016 14:19
Setup git BASH environment: prompt with the current branch and status color code (GREEN - no changes; RED - something to commit; YELLOW - something else: a new un-tracked file etc.), current directory shortening. #git #bash #nad2000
# drops first portion of a path $1 if length is greater than $2
function __droppath {
if [[ ${#1} -gt $2 ]]; then
p=$1
while [ ${#p} -gt $2 ]; do
p="/"$(echo "$p"|cut -d"/" -f3-)
done
echo "..."$p
else
echo $1
@nad2000
nad2000 / 0_reuse_code.js
Created December 28, 2013 06:25
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@nad2000
nad2000 / process_pgbench_outpus.sh
Last active December 31, 2015 04:59
1) removes all lines except relevant: - iteration parameter; - tps (excluding connection time). 2) joins lines together to form CSV file.
egrep "(\*\*\*\*|excluding)" test.log | sed '/tps/ s/tps = \([.0-9]*\) .*/\1/; /uview/ s/ uview\([0-9]*\)/\1/' | sed -n '/\**/ {N;s/\n/,/;N;s/\n/,/;s/\**//p}'
## With loading into PostgreSQL:
# Create table:
psql -d 5 -c "create table test_output( id int, tps1 numeric, tps2 numeric)"
# Extract and load data:
egrep "(\*\*\*\*|excluding)" test.log | sed '/tps/ s/tps = \([.0-9]*\) .*/\1/; /uview/ s/ uview\([0-9]*\)/\1/' | sed -n '/\**/ {N;s/\n/,/;N;s/\n/,/;s/\**//p}' | psql -d 5 -c "TRUNCATE TABLE test_output; COPY test_output FROM STDIN (FORMAT csv, DELIMITER ',', HEADER false)"
@nad2000
nad2000 / bidir2unidir.sql
Last active December 30, 2015 13:59
Create unidirectional sample set (table) from bi-directional samples
-- PostgreSQL
DROP TABLE IF EXISTS uview5_uni;
CREATE TABLE uview5_uni AS
SELECT
start_time,
CASE e WHEN 1 THEN mac_addr_1 ELSE mac_addr_2 END src_mac_addr,
CASE e WHEN 1 THEN mac_addr_2 ELSE mac_addr_1 END des_mac_addr,
CASE e WHEN 1 THEN byte_count_1 ELSE byte_count_2 END byte_count,
CASE e WHEN 1 THEN packet_count_1 ELSE packet_count_2 END packet_count,
last_time,
#%% DATA IMPORTING AND VISUALIZATION
from urllib2 import urlopen
from contextlib import closing
from os.path import basename
def download_file(url, name=None):
with closing(urlopen(url)) as u, open(name if name else basename(url), 'w') as f:
f.write(u.read())
# -*- coding: utf-8 -*-
# Original from:
# http://pyvideo.org/video/1780/transforming-code-into-beautiful-idiomatic-python
colors = ['red', 'green', 'blue', 'yellow']
names = ['apple', 'carrot', 'melon']
for color in reversed(colors):
print color