Archive for the ‘samples’ Category
Count occurrences of a word in a file?
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!!
PyParsing with Verilog
I came across this python parsing library called pyparsing through my adviser. The first thing I wanted to try implementing on this is a verilog parser. Actually, the site’ pages allows us to request a verilog parser from the author for non-commercial purposes. Anyways, thats not the point. I know and understand some verilog. So it makes more sense for me to try a verilog parser first. I have my parser for structural verilog using this module in my pbwiki page. It cannot handle many cases yet. But I took about an hour to get that code done. I spent about a day or two trying to figure out pyparsing itself. The documentation is fairly good. There are some interesting code snippets and howto in the pyparsing wiki page.
Now I am writing a full fledged verilog parser using this module. I’ve not requested for the author’s script yet. Maybe I’ll do that once I complete to figure out how good I am in using this library. And the full semantic/syntactic specification is available in IEEE explore and in Samir Palnitkar’s book on verilog HDL. I am not sure how much time I can devote to this, so it could be some time before I’ve followed this up. Meanwhile you might want to check the various samples in the pyparsing wiki.
Demo-ing python XML abilities
I have been using python for a while now, but I never have to use the XML processing capabilities of this language! Yesterday I tried writing a quick and dirty way to convert an open office worksheet into a simpler XML. Actually not much reason to do this myself. Rama was doing something with worksheets and I decided to give this a shot. And within 15-20 minutes I have a simple XML output parsed out of the huge XML that open office format already is. That is precisely why I love writing python code.
Here is the script. Don’t bother commenting if the python language constructs are ill formed. I learn very slow if my work gets done before I learnt it all!
Hello WordPress!
After looking around livejournal for some time, I decided to check this out. Heard many good things about wp. Well here I am and hopefully, here I will be.