Thursday, April 14, 2011

LogFileTailer - simple sinatra app

It is never too late to contribute. Following on my year 2009 post - sinatra - web application framework in ruby, I recently pushed that code to github @http://github.com/NiranjanSarade/LogFileTailer

Tuesday, April 12, 2011

Converting Unix newlines (LF) to Windows newlines (CR\LF)

In one of my earlier projects, I faced an issue with newline characters in csv file. The csv file generated on a unix system was uploaded to Windows system through sftp. However, the program on the Windows server was not able to parse the file properly as the csv file contained Unix newlines (LF) instead of Windows newlines (CR\LF) at the end of each line.

The problem is UNIX/Linux uses a Line Feed character (\n) as a line terminator while Windows uses Carriage Return\Line Feed pairs (\r\n).

For those who want to know what is the exact difference between CR, LF and EOL characters, here is a brief description :-

The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without advancing to the next line. This character is used as a new line character in Commodore and Early Macintosh operating systems (OS-9 and earlier).

The Line Feed (LF) character (0x0A, \n) moves the cursor down to the next line without returning to the beginning of the line. This character is used as a new line character in UNIX based systems (Linux, Mac OSX, etc)

The End of Line (EOL) character (0x0D0A, \r\n) is actually two ASCII characters and is a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as a new line character in most other non-Unix operating systems including Microsoft Windows, Symbian OS and others.

I was able to resolve this issue with 'Sed' (Stream Editor). (http://www.grymoire.com/Unix/Sed.html)


The command matches the regexp pattern ($ - ending position of line or the position just before a string-ending newline) and replaces it with '\r' (Carriage Return). So the end result would be conversion from '\n' to '\r\n' which will support windows new line format.

Friday, April 8, 2011

Setting up Oracle XE locally for your Rails app

I recently pushed 'database_initializer.rb' to github @http://github.com/NiranjanSarade/oracle_xe_database_initializer. Please check its README for more information.

This can be used to set up your schema and other database objects in your local Oracle XE database. The script assumes that you have following set up on your machine :-

1. Oracle client
2. activerecord-oracle_enhanced-adapter gem
3. Oracle XE (http://www.oracle.com/technetwork/database/express-edition/downloads/index.html)