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 / 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 / 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 / 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 / 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 / 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 / fast_file_copy.sh
Created March 21, 2014 03:22
Fast file copy using NC #nc #netcat #transfer #copy
# on the source (appbuild-vm4):
nc -l 7879 < /data/Traces/telstra_nexus.erf
# on the target
nc appbuild-vm4 7879 > /data/telstra_nexus.erf
## I have tried with compression, but with fast network it performed poorer (in my case with 11GB file 15 min vs. 1.5 min):
@nad2000
nad2000 / pattern.sh
Created June 24, 2014 01:46
Pattern matching for $1 = '6028276268421821235', $2 = '6028276268851317965', $3 = '85899200', $4 = NULL, $5 = NULL, $6 = NULL, $7 = 'byte', $8 = '10' and $1 = '6028276268421821235', $2 = '6028276268851317965', $3 = '85899200', $4 = NULL, $5 = NULL, $6 = NULL, $7 = 'byte', $8 = '10'
grep -E "\\\$1 = '[0-9]+', \\\$2 = '[0-9]+', \\\$3 = '[0-9]+', \\\$4 = '[0-9]+'" /endace/pgsql/pg_log/postgresql-24.log | sed -n "s/.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*/\1,\2,\3,\4/p"
grep -E "\\\$1 = '[0-9]+', \\\$2 = '[0-9]+', \\\$3 = '[0-9]+', \\\$4 = NULL, \\\$5 = NULL, \\\$6 = NULL, \\\$7 = 'byte'" /endace/pgsql/pg_log/postgresql-24.log | sed -n "s/.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*/\1,\2,\3,\4/p"
grep -E "\\\$1 = '[0-9]+', \\\$2 = '[0-9]+', \\\$3 = '[0-9]+', \\\$4 = NULL, \\\$5 = NULL, \\\$6 = NULL, \\\$7 = 'byte'" /endace/pgsql/pg_log/postgresql-24.log | sed -n "s/.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*'\([0-9]*\)'.*/\1,\2,\3,\4/p" | awk -F, '{print $1,$2,$3,$4, $2-$1}'
while true ; do echo -n $RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM | tr "0" "*" ; sleep "0.$RANDOM" ; echo $RANDOM$RANDOM$RANDOM ; sleep "0.$RANDOM" ;done
@nad2000
nad2000 / current_script_directory.sh
Created December 16, 2014 01:43
BASH script execution directory for either run in spawned process or sourced source
# $SD - script installation directory (e.g. /opt/tms/lib/dbmgr
# for packages it can be arbitrary location)
[ -z "$SD" -o ! -e "$SD" ] && SD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
@nad2000
nad2000 / test.c
Created December 17, 2014 04:41
Basic examples to show how to embed and extend Python in C including: creation of module in C with functions handling and building Python objects; importing and calling python functions from C.
/* Example of embedding Python in another program */
// to compile run:
// gcc -o test $(python-config --cflags) test.c $(python-config --ldflags) && ./test
#include<stdio.h>
#include "Python.h"
void initxyzzy(void); /* Forward */
main(int argc, char **argv)