Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Converting space separated output to one per line

leave a comment »

Here is the problem I was faced with. I wanted to check if I was part of a particular group and I did the shell groups command to find out.

Unbeknownst to me I was part of a tone of groups that included some complicated long names with backslashes (\) in them.

I thought it would be quick to grep for the particular group name if all the output was on separate lines instead of space separated on one line.

First I thought I can use cut command to do that but it turns out cut is not idea for this. It is more suitable if you have multiple lines of data and wanted to get the same column out of each of these. Something like output from ps.

Finally I achieved it really easily with with tr command. Here is how the solution looks like:


groups | tr ' ' '\n' | grep group_i_was_looking_for

Written by imsaar

January 13, 2012 at 1:04 pm

Posted in general

Windows: Printing Date and Time on Command Line

with one comment

I am more of a *nix (unix, linux etc) command line person but I still use windows from time to time (in this case because my macbook pro crashed).

I often want to see the progress of output to a file over time using the following linux command line:

$ date; wc -l output.txt

On windows date does not print time (you have to do ‘date \t’ to get output from it otherwise it used for changing the system date).

I finally found what I was looking for:

> echo %DATE% & echo %TIME% & wc -l output.txt

Output:

Thu 07/21/2011
13:44:55.70
375 output.txt

 

The wc command and bunch of other unix commands come from the free UnixUtils opensource package for windows. I consider UnixUtils a must have for windows and among the first thing I install on my windows machine.

Written by imsaar

July 21, 2011 at 12:44 pm

Posted in windows

Collaboration and Software Development (Part 1 of 2)

with one comment

I believe quality software development is a collaborative process and I personally consider myself a collaborative person (there could be some cognitive bias here)  I am happy to be working in an organization in which being Collaborative is a recognized as a core value.

The above assertion raises some questions:

What is collaboration?

Collaboration is the act of working together, caring about the common product and each other. Collaboration is often confused with cooperation which is different. I found the distinction cited here very well done:

Dillenbourg et al. (1995) make a distinction between cooperation and collaboration.

They define cooperative work as “... accomplished by the division of labor
among participants, as an activity where each person is responsible for a
portion of the problem solving...” They define collaboration as “…mutual
engagement of participants in a coordinated effort to solve the problem
together. They further note that work often is split also in
collaboration. But the difference is that in cooperation the task is split
(hierarchically) into independent subtasks, and in collaboration the
cognitive processes may be (heterarchically) divided into intertwined
layers.

Another concept that is similar and confusable is contribution. Which is doing your part towards the goal but not necessarily as part of a team or together.

What is the opposite of collaboration?

Sometimes helps to identify and understand the opposite of a concept to appreciate the concept itself. So what is the opposite of collaboration. Some would say Coercion but I think it is Competition.

I have seen it many times in my 10+ years of software development experience where individuals would get in a competitive mode due to internal reason like personality types or external reason like management’s scarcity mentality where people are stack ranked on basis of individual contributions.

How does competition looks like in a Software team?

  • My code is better than yours.  There should be a joint ownership of all of the code in team.
  • I don’t have time for your problem. If this is a team they should be working towards a common goal hence any problem is our problem.
  • Common use of hand waving help where you don’t really get engaged in the problem solving just try to give an idea to try to get back to “your” project/piece.
  • Rubber stamp code reviews
  • No passionate disagreement or challenging of individual assumption. This is passive competition or lack of care.
  • Criticism without owning the problem or offering a solution.

Read the rest of this entry »

Written by imsaar

December 28, 2010 at 6:40 pm

Vim: Set vim filetype to ruby in a Treetop grammar file

leave a comment »

While writing a syntax parser using ruby treetop library for a work project I discovered the need to use ruby filetype for syntax highlighting etc a file ending in .treetop (treetop uses polyglot to look for and compile the grammar on runtime but the grammar file has to end in .treetop).

Here is a nice getting started tutorial for treetop: http://po-ru.com/diary/getting-started-with-treetop/

I could have used some configuration in my .vimrc file to associate this file extension with ruby filetype but I want to try the embedded configuration which I have seen other used but never used myself.

After some search I found the reference article below and was by just adding the following file in the .treetop file I was able to desired functionality:

# vim:filetype=ruby

Ref:Embedding vim Settings in the File You’re Editing

Written by imsaar

June 7, 2010 at 4:21 pm

Posted in editor, vim

Config: .gitconfig

leave a comment »

[color]
        ui = auto
[alias]
  co = checkout
  br = branch
  st = status
  lol = log --format=\"%h (%aE %ar) -> %s\" --graph --decorate

Thanks to Arnab Deka

Written by imsaar

March 31, 2010 at 2:47 pm

Posted in configuration, git

Ruby: Add a prefix to all files in a directory

leave a comment »

I found myself looking for a way to rename all files in a directory by appending a prefix and couldn’t find a utility to do such a renaming after quick search so I wrote my own.


# Quick script to bulk prepend prefix to filenames to all files in a directory
# Also strips any spaces in the filename
# Example usage : ruby prepend_rename.rb Disc1_ C:\AudioBook\Disc1\

raise "Prefix and Directory are required" if ARGV.size < 2

prefix = ARGV[0]
dir = ARGV[1]

raise "Non-word character prefix #{prefix}" unless prefix =~ /^\w+$/
raise "No such directory: #{dir}" unless Dir.exists?(dir)

Dir.chdir(dir)
Dir.entries(dir).each do |file|
 next if File.directory?(file)
 new_name = prefix + file.gsub(/\s+/, '')
 File.rename(file, new_name)
end

Written by imsaar

February 7, 2010 at 6:11 pm

Posted in code, ruby, windows

Config: .vimrc (v2)

leave a comment »

” Plugins

” FuzzyFinder
” http://www.vim.org/scripts/script.php?script_id=1984

" FufFileRecursive
"
http://intraspirit.net/scratchpad/a-simple-fuzzyfinder-improvement/

" MatchIt for multi-character match on % (def and end)
“  REF: http://awesomeful.net/posts/57-small-collection-of-useful-vim-tricks
“  http://www.vim.org/scripts/script.php?script_id=39

” Rails.vim
” http://www.vim.org/scripts/script.php?script_id=1567

" Ali Rizvi's Vim Settings

set number
set shiftwidth=4

set incsearch
set hlsearch
set textwidth=80

” from lindes:
syntax on
hi Comment term=bold ctermfg=Cyan guifg=Cyan
set autoindent

” from benji fisher to turn on the matchit plugin automatically and more
filetype plugin on

” explicity map file extension .t to perl syntax instead of tads
” which is autodetected by filetype plugin on
” This line should always be after filetype plugin
autocmd BufNewFile,BufRead *.t set syntax=perl

” to show real tabs and spaces in file
set list
set listchars=tab:>-,trail:^,eol:$
“set listchars=tab:>-,trail:-

“to add spaces instead of tabs
set expandtab

” evil, bad! — hard tabs should be 8 chars… –lindes
” set tabstop=4
” _but_ we can use this:
set smarttab

“make the background light
set background=light

“show matching parens
set showmatch

“show row and column number
set ruler

” allow backspacing over everything in insert mode
set backspace=indent,eol,start

filetype plugin indent on

“set shiftwidth to 2 for ruby only
autocmd FileType ruby setlocal sw=2

“shortcuts inspired by http://weblog.jamisbuck.org/2008/11/17/vim-follow-up

let g:fuzzy_ignore = “*.log”
let g:fuzzy_matching_limit = 70
map fb :FufBuffer
map fd :FufDir<CR>
map ff :FufFile<CR>
” addition based on http://intraspirit.net/scratchpad/a-simple-fuzzyfinder-improvement/
map ff :FufFileRecursive<CR>
map fm :FufMruFile<CR>

silent execute '!mkdir -p ~/.vim_backups'
set backupdir=~/.vim_backups//
set directory=~/.vim_backups//

map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2
map <F3> :source ~/vim_session <cr>     " And load session with F3

Written by imsaar

February 6, 2010 at 10:19 pm

Posted in configuration, editor, vim

Config: .vimrc

leave a comment »

" Ali Rizvi's Vim Settings

set number
set shiftwidth=4

set incsearch
set hlsearch
set textwidth=80

" from lindes:
syntax on
hi Comment term=bold ctermfg=Cyan guifg=Cyan
set autoindent

" from benji fisher to turn on the matchit plugin automatically and more
filetype plugin on

" explicity map file extension .t to perl syntax instead of tads
" which is autodetected by filetype plugin on
" This line should always be after filetype plugin
autocmd BufNewFile,BufRead *.t set syntax=perl

" to show real tabs and spaces in file
set list
set listchars=tab:>-,trail:^,eol:$
"set listchars=tab:>-,trail:-

"to add spaces instead of tabs
set expandtab

" evil, bad! -- hard tabs should be 8 chars... --lindes
" set tabstop=4
" _but_ we can use this:
set smarttab

"use arrow keys to move to previous and next buffers
nnoremap  :bn
nnoremap  :bp

"make the background light
set background=light

"show matching parens
set showmatch

"show row and column number
set ruler

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

filetype plugin indent on

"set shiftwidth to 2 for ruby only
autocmd FileType ruby setlocal  sw=2

"shortcuts inspired by http://weblog.jamisbuck.org/2008/11/17/vim-follow-up
let mapleader = " "
map n :execute 'NERDTreeToggle ' . getcwd()

let g:fuzzy_ignore = "*.log"
let g:fuzzy_matching_limit = 70
map ft :FuzzyFinderTextMate
map fb :FuzzyFinderBuffer
map ff :FuzzyFinderFile =fnamemodify('**/x', ':p')
map fm :FuzzyFinderMruFile

command Ped :exec '!p4 edit %'
command Pad :exec '!p4 add %'

set backupdir=~/.vim-tmp
set directory=~/.vim-tmp

Written by imsaar

November 24, 2009 at 6:19 am

Posted in configuration

Config: .screenrc

leave a comment »

#
# Example of a user's .screenrc file
#

# This is how one can set a reattach password:
# password ODSJQf.4IJN7E    # "1234"

# no annoying audible bell, please
vbell on

# detach on hangup
autodetach on

# don't display the copyright page
startup_message off

# emulate .logout message
pow_detach_msg "Screen session of \$LOGNAME \$:cr:\$:nl:ended."

# advertise hardstatus support to $TERMCAP
# termcapinfo  * '' 'hs:ts=\E_:fs=\E\\:ds=\E_\E\\'

# make the shell in every window a login shell
#shell -$SHELL

# autoaka testing
# shellaka '> |tcsh'
# shellaka '$ |sh'

# set every new windows hardstatus line to somenthing descriptive
# defhstatus "screen: ^En (^Et)"

defscrollback 10000

# don't kill window after the process died
# zombie "^["
################
#
# xterm tweaks
#

#xterm understands both im/ic and doesn't have a status line.
#Note: Do not specify im and ic in the real termcap/info file as
#some programs (e.g. vi) will not work anymore.
termcap  xterm hs@:cs=\E[%i%d;%dr:im=\E[4h:ei=\E[4l
terminfo xterm hs@:cs=\E[%i%p1%d;%p2%dr:im=\E[4h:ei=\E[4l

#80/132 column switching must be enabled for ^AW to work
#change init sequence to not switch width
termcapinfo  xterm Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l

# Make the output buffer large for (fast) xterms.
termcapinfo xterm* OL=10000

# tell screen that xterm can switch to dark background and has function
# keys.
termcapinfo xterm 'VR=\E[?5h:VN=\E[?5l'
termcapinfo xterm 'k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~'
termcapinfo xterm 'kh=\E[1~:kI=\E[2~:kD=\E[3~:kH=\E[4~:kP=\E[H:kN=\E[6~'

# special xterm hardstatus: use the window title.
termcapinfo xterm 'hs:ts=\E]2;:fs=07:ds=\E]2;screen07'

#terminfo xterm 'vb=\E[?5h$\E[?5l'
termcapinfo xterm 'vi=\E[?25l:ve=\E[34h\E[?25h:vs=\E[34l'

# emulate part of the 'K' charset
termcapinfo   xterm 'XC=K%,%\E(B,[\304,\\\\\326,]\334,{\344,|\366,}\374,~\337'
# xterm-52 tweaks:
# - uses background color for delete operations
termcapinfo xterm be

################
#
# wyse terminals
#

#wyse-75-42 must have flow control (xo = "terminal uses xon/xoff")
#essential to have it here, as this is a slow terminal.
termcapinfo wy75-42 xo:hs@

# New termcap sequences for cursor application mode.
termcapinfo wy* CS=\E[?1h:CE=\E[?1l:vi=\E[?25l:ve=\E[?25h:VR=\E[?5h:VN=\E[?5l:cb=\E[1K:CD=\E[1J

################
#
# other terminals
#

#make hp700 termcap/info better
termcapinfo  hp700 'Z0=\E[?3h:Z1=\E[?3l:hs:ts=\E[62"p\E[0$~\E[2$~\E[1$}:fs=\E[0}\E[61"p:ds=\E[62"p\E[1$~\E[61"p:ic@'

# Extend the vt100 desciption by some sequences.
termcap  vt100* ms:AL=\E[%dL:DL=\E[%dM:UP=\E[%dA:DO=\E[%dB:LE=\E[%dD:RI=\E[%dC
terminfo vt100* ms:AL=\E[%p1%dL:DL=\E[%p1%dM:UP=\E[%p1%dA:DO=\E[%p1%dB:LE=\E[%p1%dD:RI=\E[%p1%dC

################
#
# keybindings
#

#remove some stupid / dangerous key bindings
bind k
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h
#make them better
bind 'K' kill
bind 'I' login on
bind 'O' login off
bind '}' history

# Yet another hack:
# Prepend/append register [/] to the paste if ^a^] is pressed.
# This lets me have autoindent mode in vi.
register [ "33:se noai15a"
register ] "33:se ai15a"
bind ^] paste [.]

################
#
# default windows
#

# screen -t local 0
# screen -t mail 1 elm
# screen -t 40 2 rlogin faui40
# caption always "%3n %t%? @%u%?%? [%h]%?"
# hardstatus alwaysignore
# hardstatus alwayslastline "%w"

# bind = resize =
# bind + resize +1
# bind - resize -1
# bind _ resize max
#
# attrcolor u "-u b"
# attrcolor b "R"

# fix delayed vi/vim startup in screen
altscreen on

# something that would reload the .screenrc file on reattach
bind R source $HOME/.screenrc

# http://www.ibm.com/developerworks/aix/library/au-gnu_screen/index.html
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

Written by imsaar

November 24, 2009 at 6:10 am

Posted in configuration

Ruby: String += vs <<

with 2 comments

Learned this interesting tidbit on Seattle.rb mailing list that appending a string with << is 10 times more efficient than +=. This would matter if you are doing a lot of string concatenation.

From: Joe Van Dyk
Date: Fri, Nov 20, 2009 at 10:20 AM
Subject: Re: [Ruby] += vs <<
To: Seattle Ruby Brigade!

$ cat t.rb
require 'benchmark'

long_string = 's' * 100000000
another_long_string = 'y ' * 1000

Benchmark.bm do |b|
b.report("<<") { long_string << another_long_string }
b.report("+=") { long_string += another_long_string }
end

$ ruby t.rb
user system total real
<< 0.000000 0.020000 0.020000 ( 0.022544)
+= 0.110000 0.130000 0.240000 ( 0.250046)

Update: Later in this thread mentioned a comprehensive blog entry about this topic : The Fastest Way to Concatenate Strings and Arrays in Ruby

Written by imsaar

November 20, 2009 at 8:44 pm

Posted in ruby

Follow

Get every new post delivered to your Inbox.

Join 168 other followers