How to sort a file by its columns

Posted by root Fri, 05 Jun 2009 01:29:00 GMT

# sort by user id
ruby -a -F: -ne 'puts $F.values_at(2, 0).join("\t")' /etc/passwd | sort -g

# sort by user home directory
ruby -a -F: -ne 'puts $F.values_at(5, 0).join("\t")' /etc/passwd | sort -g

# sort by username
ruby -a -F: -ne 'puts $F.values_at(0).join("\t")' /etc/passwd | sort -g

Posted in ,  | Tags  | no comments | no trackbacks

Viva Liverpool!

Posted by root Sun, 31 May 2009 20:26:00 GMT



Posted in  | no comments | no trackbacks

HowTo edit the ReCaptcha look

Posted by root Fri, 01 May 2009 20:52:00 GMT

Take a look at this .js – https://api-secure.recaptcha.net/js/recaptcha.js

Posted in  | Tags  | no comments | no trackbacks

Simple Ruby Text Parser

Posted by root Fri, 20 Mar 2009 15:42:00 GMT

#!/usr/bin/ruby

filename = "sample records.txt"
file         = File.open(filename)
data       = []
record    = {}

class MyParse
  Tokens = [
    ### change the regular expressions accordingly ###
    ### <sale date> <sale time> <sale address>
    [/^Sale:\s*(\d+\/\d+\/\d+)\s+(\S+\s+\w\w)\s+(.+?)\s*$/,
        lambda { |m| m.to_a && 
            {"sale date"=>m[1], "sale time"=>m[2], "sale address"=>m[3] } }],
    ### <seller name> <seller time> <seller address>
    [/^Seller:\s*(\d+\/\d+\/\d+)\s+(\S+\s+\w\w)\s+(.+?)\s*$/,
        lambda { |m| m.to_a && 
            {"seller date"=>m[1], "seller time"=>m[2], "seller address"=>m[3] } }],
    ### Trustor: <trustor>
    [/^Trustor:\s+(.+?)\s*$/,
        lambda { |m| m.to_a && {"trustor"=>m[1] } }],
  ]

  def self.read(text)
    parse(text)
  end

  protected
  def self.parse(text)
    text.each do |line|
      Tokens.each do |token|
        if m = token.first.match(line)
          return token.last.call(m)
        end
      end
    end
    nil
  end

end

begin
  while line = file.readline
    if line.match(/^\s*$/)
      record = {}
      next
    end
    line.sub!("\r", "")
    if record = MyParse.read(line)
      data.push(record)
    end
  end
rescue EOFError => e
  puts "'#{filename}': #{e}"
ensure
  file.close
end

puts data.inspect

exit(0)

Posted in  | Tags  | no comments | no trackbacks

A Quick Remedy for ipod shuffle Illnesses

Posted by root Thu, 19 Mar 2009 01:02:00 GMT

Symptoms: one green light flash, followed by two green ones.
Conclusion: the player is dead.
Remedy: download the reset utility, reset the player and

# change _path_to_the_device_ with the real path (see `dmesg|tail`)

mount /dev/_path_to_the_device_  /mnt && cd /mnt

# download rebuild_db.py from http://shuffle-db.sourceforge.net/
# copy 'rebuild_db.py' in /mnt and create the dirs' structure

python -c '
import os
os.makedirs("iPod_Control/iTunes")
os.makedirs("Music")
' 

cd /mnt && cp ~/Music/*.mp3 Music
python rebuild_db.py && cd .. && umount

That’s it.

P.S. you can format this ipod as many times as you like,
just follow the above procedure to reset it.

Posted in  | Tags  | no comments | no trackbacks

howto download an image from flickr

Posted by root Wed, 11 Mar 2009 20:10:00 GMT

I wasn’t able to download these cats from flickr. It happened to be 1px transparent image over the other. Here is a simple scaper in cases of such “protection”:
require 'rubygems'
require 'www/mechanize'

url = %q[http://www.flickr.com/photos/46457493@N00/3162339409/in/set-72157612102050814/]

my_id = url.split("/")[-3]
agent  = WWW::Mechanize.new
page   = agent.get(url)

url   = page.search("//div[@id='photoImgDiv#{my_id}']/img").attr('src')

agent.get(url).save_as((File.basename(url)).split("?")[0])

Posted in ,  | Tags  | no comments | no trackbacks

Code refactoring

Posted by root Sat, 21 Feb 2009 11:40:00 GMT

An example (The Bible, Genesis 1)

1: In the beginning God created the heaven and the earth.
...
4: And God saw the light, that it was good: and God divided the light from the darkness.
... and God saw that it was good.
... and God saw that it was good.

and so on …

Posted in  | no comments | no trackbacks

iptables - blocking incoming traffic

Posted by root Sat, 14 Feb 2009 16:28:00 GMT

This iptables config will block all incoming traffic, except from machines, identified by thier MAC addresses:

iptables -F INPUT
iptables -A INPUT -m mac—mac-source 00:3f:5f:ab:2c:7d -j ACCEPT
iptables -A INPUT -m mac—mac-source 00:1e:32:36:97:4a -j ACCEPT
iptables -A INPUT -m mac—mac-source 00:44:11:a5:1d:6d -j ACCEPT
 # or by ip address:
 #iptables -A INPUT -p tcp -s 1.1.1.1 -j ACCEPT
 #iptables -A INPUT -p tcp -s 2.2.2.2 -j ACCEPT
 #iptables -A INPUT -p tcp -s 3.3.3.3 -j ACCEPT
iptables -A INPUT -m state—state ESTABLISHED -j ACCEPT
iptables -A INPUT -s 127.0.0.1 -j ACCEPT
iptables -A INPUT -j REJECT

Posted in  | Tags  | no comments | no trackbacks

PS1 colors

Posted by root Sat, 14 Feb 2009 09:11:00 GMT

I like to see green prompt for userid > 0 and red for root, also the hostname and the history number of a command.

PS1 for userid != 0
PS1=”\[\e[32;1m\]\u\[\e[30;1m\]@\[\e[32;1m\]\H[\!]\W\[\e[30;1m\]\\$\[\e[0m\]”

PS1 for userid == 0
PS1=”\[\e[32;1m\]\u\[\e[30;1m\]@\[\e[32;1m\]\H[\!]\W\[\e[30;1m\]\\$\[\e[0m\]”

Posted in  | Tags  | no comments | no trackbacks

just another perlmonks' post

Posted by root Fri, 06 Feb 2009 10:16:00 GMT

perldoc perlfaq1 | perl -ne ’/((?<=”)(.+hack…(?:OR)?))/&&die$2.$/’ -

Posted in  | Tags  | no comments | no trackbacks

Older posts: 1 2 3 ... 11