Skip to content

Instantly share code, notes, and snippets.

View wteuber's full-sized avatar

Wolfgang Teuber wteuber

View GitHub Profile
tokens = [/^:61/, /^:62F:/]
mt940 = <<MT940
:20:951110
:25:45050050/76198810
:28:27/01
:60F:C951016DEM84349,74
:61:951017D6800,NCHK16703074
:86:999PN5477SCHECK-NR. 0000016703074
:61:951017D620,3NSTON
#!/usr/bin/env ruby
# encoding: utf-8
require 'yaml'
require 'active_support/hash_with_indifferent_access'
class Hash
def self.new_from_hash_copying_default(hash)
hash = hash.to_hash
new(hash).tap do |new_hash|
@wteuber
wteuber / add_newline.sh
Last active August 29, 2015 14:05
No newline at end of file? These bash snippets will help you add new line at end of files.
#define file extensions that should be regarded
EXT=(css ejs erb gemspec gitignore html js json md mustache rake rb rdoc rspec ru scss sh sql txt ui xml xsd yardopts yml)
EXT=$(printf "\|%s" "${EXT[@]}")
EXT=${EXT:2}
# git:
git ls-files "*.*" | grep "\.\($EXT\)$" | sed "s/^/\'/; s/$/\'/;" | xargs sed -i -e '$a\'
@wteuber
wteuber / list_file_extensions.sh
Last active August 29, 2015 14:05
list file extensions
# plain
find . -name '*.*' | sed 's/.*\.//' | sort -u | tr "\n" " "
# git
git ls-files "*.*" | sed 's/.*\.//' | sort -u | tr "\n" " "
# Explained
# find . -name '*.*' # List all files containing a '.'
# git ls-files "*.*" # List all tracked files containing a '.'
@wteuber
wteuber / configurable_modules.rb
Last active August 29, 2015 14:06
configurable in ruby modules
module Foo
include ActiveSupport::Configurable
class << self
def configure
yield config
end
end
end
Foo.configure do |config|
@wteuber
wteuber / hex_dump_restore.sh
Created September 18, 2014 07:30
Hex dump/restore binary file
# cat, print file.bin's binary content
cat file.bin
# =>
# DUMP - convert binary content to hex string
xxd -p file.bin | tr -d '\n'
# => 48656c6c6f20576f726c640a
# RESTORE - convert hex string to binary content
echo 48656c6c6f20576f726c640a | xxd -r -p
@wteuber
wteuber / gist:ab9b30069160737bfcb3
Created September 23, 2014 10:44
DUMP / RESTORE Ubuntu packages
# DUMP
mkdir -p ~/backup
dpkg --get-selections > ~/backup/Package.list
sudo cp -R /etc/apt/sources.list* ~/backup/
sudo apt-key exportall > ~/backup/Repo.keys
# RESTORE
sudo apt-key add ~/backup/Repo.keys
sudo cp -R ~/backup/sources.list* /etc/apt/
sudo apt-get update
@wteuber
wteuber / hash_slice.rb
Created April 8, 2015 09:11
slice ruby Hash
params = Hash[[*'a'..'j'].zip([*1..10])] # => {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5, "f"=>6, "g"=>7, "h"=>8, "i"=>9, "j"=>10}
slice_keys = ['a','f','g']
filter = -> (key, _) { slice_keys.include?(key) }
slice = params.select &filter
params.delete_if &filter
slice # => {"a"=>1, "f"=>6, "g"=>7}
params # => {"b"=>2, "c"=>3, "d"=>4, "e"=>5, "h"=>8, "i"=>9, "j"=>10}
@wteuber
wteuber / equal_files.rb
Created April 29, 2015 08:27
find equal files recursively
require 'digest'
result = Hash.new() { |hash, key| hash[key] = [] }
Dir['**/*'].each do |file|
if File.file?(file)
result[Digest::MD5.new.file(file).to_s] << file
end
end
result
ls **/en.yml **/de.yml | grep -v spec| tar -czf archive.tgz -T -