Thursday, December 3, 2009

Calculate checksum with Perl

Swedish personal and organization identity number consists of 10 digits and the tenth digit is the checksum. This Perl script calculates checksum for the identity number.

#!/usr/bin/perl

die "checksum.pl identity\n" unless @ARGV > 0 && length $ARGV[0] == 9;

sub checksum
{
  my @pnr = split(//, shift);
  my $n = 2;
  my $sum;

  foreach (@pnr) {
    my $tmp = $_ * $n;
    if($tmp > 9) { $sum += 1 + ($tmp % 10) }
    else { $sum += $tmp; }

    $n = ($n == 2) ? 1:2;
  }

  $tmp = substr($sum, length($sum) -1);
  return ($tmp == 0) ? 0 : (10-$tmp);
}

print $ARGV[0], checksum($ARGV[0]), "\n";

Friday, November 27, 2009

Search file contents on Unix computers

To search files by file content on a Unix computers, you can use a text search utility called grep. This sample searches in data directory and all subdirectories for text files containing keyword sample.

grep --reqursive --include "*.txt" sample /data

or

find /data -name "*.txt" | xargs grep sample

Saturday, July 25, 2009

Windows 7

I have downloaded and installed Windows 7 RC 1 on a Samsung Net PC NC10. After three weeks I releized Windows 7 is much better operating system than Windows XP and Vista. Especially then you have a computer with limited resources like CPU and memory.

Wednesday, January 21, 2009

A world of music

Now! I have found a commercial music service on Internet I am really like. After you have downloaded the software and paid for an account, you can search and play music for hours. Spotify is perfect, they support Mac OS X, Growl and progressive rock music like Saga, Steve Hackett and Camel.

Monday, January 5, 2009

GeekTool for Apple Mac OS X

GeekTool is a excellent tool for Mac OS X to show text files or output from unix commands on your desktop without opening a Terminal window. I am using the tool to execute commands and perl scripts to monitor my computer status.



This command displays current network address for my computer.

netstat -n| awk '{print $4}' | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | head -n 1

For more information click here.