jQuery bind function and javascript detect enter key

jQuery bind function:

$ ('#searchbox input').bind('keypress', function(e) {});

For detecting the ‘enter’ key pressed, write in script,

var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
   //Do something
 }
Advertisements

Linux: How to split large files into several parts

HJSplit

HJSplit for Linux is a freeware file splitter for Linux with full graphical user-interface.
Download ‘HJSplit’ from Freebytesoftware

Unix Split command

$ split -l 1000 my_file.txt     # Split the file named my_file.txt into several files with 1000 lines 
$ split -b n my_file.txt	# Split a file into pieces n bytes in size. 

Create, write and read a file and also print the first line of a file in ruby

Create a file: Open a file in write mode

ruby-1.9.2-p290 :058 > f = File.open(“my_file.txt”, “w”)
=>

Write something to that file

ruby-1.9.2-p290 :059 > f.write(“This is the first line of my file\n”)
=> 34

Close the file

ruby-1.9.2-p290 :060 > f.close
=> nil

Open the file for reading

ruby-1.9.2-p290 :061 > f = File.open(“my_file.txt”, “r”)
ruby-1.9.2-p290 :062 > f.read
=> “This is the first line of my file.\n”

Print the first line of a file

ruby-1.9.2-p290 :063 > lines = IO.readlines(“my_file.txt”)
ruby-1.9.2-p290 :063 > puts lines.first
This is the first line of my file.

A useful link about ruby File manupulation is given below

Open ods, xls, xlsx files in ruby by using ‘roo’

Install ‘roo’ gem
$ sudo gem install ‘roo’
take the ruby console
$ irb
for example take an xlxs file and try to open

ruby-1.9.2-p290 :003 > require ‘roo’
if you get an error like
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:6: warning: already initialized constant VERSION
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:7: warning: already initialized constant LIBPATH
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:8: warning: already initialized constant PATH
LoadError: no such file to load — zip/zipfilesystem
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo/openoffice.rb:3:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:71:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from (irb):3
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `’
ruby-1.9.2-p290 :004 >

Then install the ‘rubyzip2’ gem

$ gem install rubyzip2

and open the file
ruby-1.9.2-p290 :003 > file = Excelx.new(“/home/prescience-toshiba/Documents/abhilash.raw.xlsx”)

output:
=> {[1, 1]=>”Name”, [1, 2]=>”Email Id”, [1, 3]=>”Alternate Number”, [1, 4]=>”Date of Birth”, [1, 5]=>”Mobile No.”, [1, 6]=>”Functional Area”, [1, 7]=>”Area of Specialization”, [1, 8]=>”Industry”, [1, 9]=>”Resume Title”, [1, 10]=>”Key Skills”, [1, 11]=>”Work Experience”, [1, 12]=>”Current Employer”, [1, 13]=>”Previous Employer”, [1, 14]=>”Current Salary”, [1, 15]=>”Level”, [1, 16]=>”Current Location”, [1, 17]=>”Preferred Location”, [1, 18]=>”Course(Highest Education)”, [1, 19]=>”Name”, [1, 20]=>”Email Id”, [1, 21]=>”Alternate Number”, [1, 22]=>”Date of Birth”, [1, 23]=>”Mobile No.”, [1, 24]=>”Functional Area”, [1, 25]=>”Area of Specialization”, [1, 26]=>”Industry”, [1, 27]=>”Resume Title”, [1, 28]=>”Key Skills”, [1, 29]=>”Work Experience”, [1, 30]=>”Current Employer”, [2, 1]=>”Rahel Robert”, [2, 2]=>”rtrobert@yahoo.com”, [……………………..

Ruby gem roo error: LoadError: no such file to load — zip/zipfilesystem

ruby-1.9.2-p290 :003 > require ‘roo’
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:6: warning: already initialized constant VERSION
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:7: warning: already initialized constant LIBPATH
/home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:8: warning: already initialized constant PATH
LoadError: no such file to load — zip/zipfilesystem
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo/openoffice.rb:3:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/gems/ruby-1.9.2-p290@rails31rc/gems/roo-1.10.0/lib/roo.rb:71:in `’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from (irb):3
from /home/prescience-toshiba/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `’
ruby-1.9.2-p290 :004 >

Instead of roo do

ruby-1.9.2-p290 :004 > require ‘roo/excel’

This works fine. If you want zip gem then install it.

$ gem install rubyzip2

Then do

ruby-1.9.2-p290 :001 > require ‘roo’

=> true