Skip to content

Instantly share code, notes, and snippets.

View amosshapira's full-sized avatar

Amos Shapira amosshapira

  • Sydney, Australia
View GitHub Profile
@amosshapira
amosshapira / replace-plus-with-urlconde
Last active August 29, 2015 13:56
Replace all "+" characters in file names to "%2b" url encoded
What I like about this way of doing this is that it takes advantage of Bash internals without external commands.
for i in *; do mv $i ${i//+/%2b}; done
xmllint --format -
("-" for standard input)
@amosshapira
amosshapira / osx-number-of-cpus
Created June 23, 2014 03:53
Find number of CPU's on OSX
From https://groups.google.com/d/msg/gnu.emacs.help/gr5lC8sPyks/SNx6JblcuvgJ:
sysctl -n hw.logicalcpu_max
@amosshapira
amosshapira / .gvimrc
Created July 4, 2014 00:53
.gvimrc on Mac
set guifont=Menlo\ Regular:h18
set number
" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set noexpandtab
au BufRead,BufNewFile *.h set noexpandtab
au BufRead,BufNewFile Makefile* set noexpandtab
" --------------------------------------------------------------------------------
Emscripten does not work with LLVM 3.3, which is provided with Fedora 19.
Download LLVM 3.2 and Clang from http://llvm.org/releases/. Copy the clang
source code into tools\clang, and run make (but not make install).
Set LLVM_ROOT='/home/yourname/Development/llvm-3.2.src/Release+Asserts/bin' in ~./emscripten.
sudo yum install nodejs - I found that chromium installed later versions
of the v8 package, which meant that nodejs would not install. So download
Node.js from http://nodejs.org/download/.
@amosshapira
amosshapira / lastlines.c
Last active August 29, 2015 14:05
Just a silly exercise in implementing the most efficient way to print the last X lines of a file.
/* most efficient way to print the last X lines of a file.
*
* it's done using my favourite system call - mmap(2). Instead of reading the file
* from start to finish and count newlines, the program maps the file into memory and
* scans it backwards from the end, counting newline characters as it goes. When it decided
* thet it found the beginning of the X line from the end (or reached the beginning of the file)
* it prints the entire block of lines in one call.
*
* Options to improve:
* 1. Use direct call to write(2) on file descriptor 1 instead of fwrite()
@amosshapira
amosshapira / quoted-csv.awk
Created September 25, 2014 02:05
Extract double-quoted fields from CSV file using GNU Awk
#!/usr/local/bin/gawk -f
# Taken from http://stackoverflow.com/a/8949920/164137
BEGIN {
FS=","
FPAT="([^,]+)|(\"[^\"]+\")"
}
{
@amosshapira
amosshapira / ssh-copy-id
Created September 30, 2014 05:50
ssh-copy-id for mac (perhaps cross-platform)
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
ID_FILE="${HOME}/.ssh/id_rsa.pub"
if [ "-i" = "$1" ]; then
@amosshapira
amosshapira / test-files-with-spinner
Created November 6, 2014 01:34
Read file names from stdin, verify that they are regular files. The nice thing about this is the in-place progress spinner
#!/bin/bash
spin='-\|/'
i=0
ceol=`tput el`
while read filename
do
if [[ -f $filename ]]
then
i=$(( (i+1) % 4 ))
@amosshapira
amosshapira / Vagrantfile
Created December 9, 2014 10:14
Vagrant file reading node dat afrom Hiera
boxes = Dir.glob("puppet/hiera/nodes/*.yaml").map {|f| YAML.load(File.read(f)) }
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
boxes.each do |attrs|
# Required attributes
boxname = attrs['box']