Tech Talk

Linux, Python, E51, Scripting…

Archive for April 2008

Count occurrences of a word in a file?

with 2 comments

Tricky question. My friend came up with a request to count the number of occurrences of a word in a text file, without too much of a script. A single line solution? One that pops at the top of the head is grep -c. But that will fail if the word repeated within the line! With so many text processing commands in linux, I was left wondering what works easiest.

I haven’t used awk/sed/perl in a while. I could write him a python script in just about the typing time. But how should I do it without any of these? With the constraint that the words are separated by spaces, I ended up with this,


$ tr [:blank:] ['\n'] < file.txt | grep -c "pattern"

Seems to work. Not sure if there is an obvious mistake laughing at me. Maybe python can be harmful to your old linux habits!!

Blogged with the Flock Browser

Tags: , , , , , ,

Written by abiya

April 29, 2008 at 6:35 am

Posted in python, samples, technical

How not to write software

with one comment

No, this is not another preaching or top ten tips or anything. This is about something I loved when I first understood it, felt people didn’t know to use it when I started using it and now feel is a bad design example. There are two facets to it : the windows registry and the nokia pc suite.

I knew about windows registry when I first started learning Visual Basic about 8 years ago. It felt exciting. You could actually register a service and some client could look it up and use much like the today’s hyped WSDL and SOA stuff. I was not a programmer then. Was just another curious kid trying to understand what my machine has installed.

After a while, I came across a whole lot of demo software that you could continue using by changing some entries in this registry. I felt that the programmers of these tools were missing something, or the entire idea of a demo is just a hype. Again it was too early for me to say anything about this. I just did not know enough of the software business then.

About three years back, I picked my first Nokia mobile phone. It came with this software called PC suite. At that time, I was as excited about it as I was about using windows registry. This was a time when I just started using linux. I used to regularly login to windows since I was not entirely comfortable with the toolset in linux and this nokia pc suite did not ship a linux version. It still doesn’t, but that is another issue.

But the pc suite experience left me wondering if it was written by software programmers. I was working in a software outsourcing co at this time and did understand something about software design and principles. But I found pc suite application very stupid. I seemed to eat a lot of my machine’s resources while trying to sync contacts with my phone, or transferring images from the phone. I have many times cursed the app and quit without doing anything.

Last week when I picked E51, the pc suite developers proved their capabilities. Pc suite wouldn’t install because the previous PC suite version was not completely uninstalled. WTH, you wrote both the versions, won’t you know how to upgrade between them? It left me irritated. I even thought I’ll return the phone to nokia and request them to hire some software engineers to do the user installation end of stuff also.

The issue : PC suite makes one million entries in various names across the entire registry making it almost impossible to remove them all. In fact, it must have gotten so out of their hands, that they even had a PC suite clean up application! Can you beat that? Anyways, after a couple of registry edits a lot of curses and about three restarts, the thing finally worked. Now I understand why they don’t ship PC suite for linux. You perhaps can never make it so complicated. Windows registry and Nokia PC suite combo is one perfect example for a “spaghetti pattern”, an anti-pattern that most software guys try to avoid. Do they really?

The reason for irritation has its seeds elsewhere. I have not used windows a lot in the last two years. I have a debian dual boot and am loving it. Installing something in linux can get just as painful, but most of the times, the issue is just unresolved dependancy. Rarely unresolvable. But in windows, they neither tell you what dependancy failed, nor what you can do to resolve them. You need somebody who speaks in “registry” to resolve the issue. Maybe this is because I don’t develop in a windows environment, but the user friendly part is totally lost. Why else is anyone using windows?

Blogged with the Flock Browser

Tags: , , , , , ,

Written by abiya

April 17, 2008 at 4:40 am