Friday, March 14, 2008

Using awk to sum all entries in one column

Had to search this one for a while... here is the source file:

360109368 15004557 java.lang.String
310090432 7692644 char[]
179389488 7474562 java.util.HashMap$Entry
71350968 70689 java.util.HashMap$Entry[]
14298360 109019 * ConstMethodKlass
7853616 109019 * MethodKlass
7409816 119136 java.lang.Object[]
7302080 17556 int[]
7094864 11288 byte[]

I wanted the sum of all the numbers in the first column and heres the awk line I used:

cat sourcefile | awk '{print $1}' | awk 'BEGIN{sum=0}{sum+=$1}END{print "Total: " sum}'

Wednesday, March 12, 2008

MySQL on Leopard for rails


SQLite is a great little tool and very easy to use and setup… gem install sqlite3-ruby and you are done!! But now I’m creating a rails application that needs foreign keys and that’s something that SQLite does not do. So MySQL it is…(I’m a MySQL guy if you haven’t figured)

My choice to install MySQL on the macbook was using macports… I’ll assume that macports is installed correctly for the next exercises…  (port version should give you the version number)
Installing MySQL 5.0 with port:

sudo port install mysql5 +server

When the installation is done you have to create the run directory

sudo mkdir /opt/local/var/run
sudo mkdir /opt/local/var/run/mysql5
cd /opt/local/var/run
sudo chown _mysql mysql5
ls -al
total 0
drwxr-xr-x  4 _mysql  admin   136B Mar 12 10:37 mysql5/


setup the config file for MySql. (use the template so you can see where Mysql puts stuff)

sudo /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/mysql5/my.cnf

Testing if MySQL Starts…

sudo /opt/local/lib/mysql5/bin/mysqld_safe5

(you will need crtl-Z and then type bg. This will put the MySQL process in the background. type jobs to see the process)

now connect to mysql with the client

mysql5 -uroot

If you want to stop MySQL you have to kill the process. ps -ax | grep mysqld… there should be one mysqld and one mysqld_sfae5 process get BOTH pids and run kill pid pid.

ps -ax | grep mysqld 70170   ??  S      0:00.01 /bin/sh /opt/local/lib/mysql5/bin/mysqld_safe --datadir=/opt/local/var/db/mysql5 --pid-file=/opt/local/var/db/mysql5/new-host-2.home.pid
70195   ??  S      0:01.22 /opt/local/libexec/mysqld --basedir=/opt/local --datadir=/opt/local/var/db/mysql5 --user=mysql --pid-file=/opt/local/var/db/mysql5/new-host-2.home.pid --port=3306 --socket=/opt/local/var/run/mysql5/mysqld.sock kill  70170 70195

If you only kill one the server will auto restart!!
Running MySQL using MySQL launchctl

This is where I need more help BUT this is what I did and it works:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist  
PS(make sure you kill mysqld_safe5 before doing this...)

Now the part I don’t udnerstand… the server just started but I don’t know how to CONTROL the server like stop, start and restart???

sudo launchctl stop org.macports.mysql5

This did "restart" MySQL but using start/restart did nothing. Looks like when you stop MySQL it aut restart from lunched. 

Password for MySQL root user?

mysqladmin5 -u password 'secret-password'

connecting with password

mysql5 -uroot -p