Skip to content

Instantly share code, notes, and snippets.

@emasaka
emasaka / ltsv.clj
Last active December 12, 2015 08:39
LTSV parser by Clojure
;; LTSV (Labeled Tab-separated Values) parser by Clojure
(use '[clojure.string :only [split]])
(defn ltsv-parse-line
"Parses one LTSV line. Returns a map."
[^String line]
(reduce (fn [r s] (let [[k v] (split s #":" 2)] (assoc r k v)))
{} (split line #"\t") ))
@emasaka
emasaka / autonot.rb
Created December 14, 2012 08:40
! someobject.foo? -> someobject.not_foo?
# inspired by http://www.msng.info/archives/2012/12/php-not.php
module AutoNot
def method_missing(method, *args)
if /\Anot_(.*\?)\z/ =~ method
! send($1, *args)
end
end
end
@emasaka
emasaka / send-to-emacs
Created December 1, 2012 07:10
nautilus script: send to running emacs
#!/bin/sh
# nautilus script: send to running emacs
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | xargs -I XX sh -c 'emacsclient -e "(find-file \"XX\")" || gdialog --msgbox "coluld not open XX"'
@emasaka
emasaka / pn.sh
Created November 6, 2012 02:35
print perfect numbers by dc (in sh)
#!/bin/sh
exec dc -e "[d]sd[q]sq[p]sp[1+dljr%0=ddlg>a]sa[+z1<s]ss[dsj2/sg0laxs_z1=qlsxdlj=p]sx[lilxxclid1+silz!<l]sl$1sz1sillx"
#!/bin/bash
get_col() {
local str=$1 fs=$2 keycol=$3
local -a a
IFS=$fs a=($str)
echo ${a[$keycol]}
}
cmp_str() { [[ $1 < $2 ]]; }
# answer of http://d.hatena.ne.jp/Yamashiro0217/20120727/1343371036
## q1
cat log.txt
## q2
cut -d , -f 1,4 log.txt
## q3
grep ^server4 log.txt
## q4
wc -l < log.txt
## q5
(require 'hi-lock)
(defun grepf->regexp (file)
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-max))
(when (looking-back "\n")
(delete-char -1) )
(goto-char (point-min))
(while (search-forward "\n" nil t)
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
def scrape
html = open('http://forkwell.com/') {|io| io.read }
Nokogiri::HTML(html, 'utf-8').css('div.grid-unit').map do |sect|
title = sect.css('header h1 a')[0]['alt']
items = []
(defvar marusuji-ary ["⓪" "①" "②" "③" "④" "⑤" "⑥" "⑦" "⑧" "⑨"
"⑩" "⑪" "⑫" "⑬" "⑭" "⑮" "⑯" "⑰" "⑱" "⑲"
"⑳" "㉑" "㉒" "㉓" "㉔" "㉕" "㉖" "㉗" "㉘" "㉙"
"㉚" "㉛" "㉜" "㉝" "㉞" "㉟" "㊱" "㊲" "㊳" "㊴"
"㊵" "㊶" "㊷" "㊸" "㊹" "㊺" "㊻" "㊼" "㊽" "㊾" "㊿" ])
(defun region-to-marusuji (beg end)
(interactive "r")
(let ((n (string-to-int (buffer-substring beg end))))
(when (and (<= 0 n) (<= n 50))
#!/bin/bash
udisks --monitor | while read ev; do
if [[ "$ev" == changed:* ]]; then
dev=${ev##*/}
if [[ "$dev" == *_* ]]; then
dev=$(echo -e ${dev//_/\\x}) # "_2d" -> "-"
fi
mp=$(udisks --show-info "/dev/$dev" | sed -n '/^ mount paths: */{s///;p}')
if [ -n "$mp" ]; then
notify-send mounted "$dev -> $mp"