Friday, January 28, 2005

Geeky Friday: find, xargs, grep, and spaces

On linux/unix systems, the find command is often used along with grep to find text in a file somewhere in a multi-level directory hierarchy. Two ways to do this are:
find . -type f -exec grep "blah" {} \;
find . -type f -print | xargs grep "blah"
The second method is slightly better as the grep command will be run only once on all files found whereas the first will run grep for each file.

The second method breaks down if find finds filenames containing spaces. If, for example, there's a file named "How To Conquer The World.txt". The xargs grep part will try to search in five files: "How", "To", "Conquer", "The", "World.txt" and grep will complain that these files don't exist.

But wait, it's 2005, surely the geeks of the world have resigned themselves to the fact that some people like to put spaces in file names -- not out of malice towards linux geeks, but just because they don't know what it can do. The good news is that yes, in fact, the geeks working on find and xargs have done something about this.

Their solution is to provide an alternate file name delimiter. The new way to handle this is to do this:

find . -type f -print0 | xargs -0 grep blah
The -print0 tells find to output its list of files separated by nul (ASCII 0) instead of a space. Similarly, the -0 tells xargs to expect that its input is separated by nulls

Friday, January 21, 2005

Geeky Friday: The Daily WTF

I've had the occasion in my career to see some really nasty code. I hope I haven't written much. If you enjoy the good feeling that comes with laughing at other people's bad code, you'll like The Daily WTF (or add the RSS feed to your aggregator).

Here's a beautiful example:

    boolean exceptionFlag = true;
    try {
     exceptionFlag = false;
    } catch(Exception e) {
     if(exceptionFlag) e.printStackTrace();

    if(exceptionFlag) throw new Exception("An Error has occured");
(Sorry non-geeks, it'd take too long to explain the humor in this)

Thursday, January 13, 2005

Geeky Friday (but not as geeky as usual): Aggregator Bliss

You may have noticed the little icons around the 'net lately. They're sitting there just taunting you about a much better way to keep up to date on your favorite sites — you need to learn about and use an aggregator.

I started using an aggregator last summer and I think it's so cool that I wrote an article explaining how it has improved my surfing, what it does and how to get started.

Monday, January 10, 2005

Clever programmers

You may already know that if you search Google for a UPS tracking number, you'll get a quick link to track the package. Well, this morning I checked on a package and found that some enterprising programmer has added a lot of keywords to a porn site so that the gibberish that makes up the tracking number actually finds a page match. Caveat emptor: I haven't clicked through to that site, so I cannot vouch for what visiting it may do to your eyeballs, computer or anything else. (I also found it interesting that Google's spell-checking engine suggested an alternative spelling for "306 12".)

Friday, January 07, 2005

Geeky Friday: Make Firefox Faster

By way of Hack a day, step by step instructions for making Firefox start to load links on the page your looking at before you actually click on any of them.

Place The State

Strangely addictive geographic game - Place The State. At the end of my best turn I had an average error of only 2 miles and the only state I missed was trying to place Utah on the board for the first move. Other turns I wasn't paying attention toward the end and did stupid things like misplacing Alaska by 100 miles or releasing the mouse too soon when placing Wyoming on turn 49 (the only other missing state was Michigan).

Note that this is the intermediate version of the game. Since it keeps the states you've already placed, it becomes easier with each turn to place the remaining states. There's an advanced version of this game available at the front page of the site. In the advanced game, you have to place each state independently. I tried it once and finished with an average error of 25 miles.

Next I'll try one of the "place the country games".

Thursday, January 06, 2005

Car adapters for iPods

I got an iPod for Christmas, so I'm now noticing articles like this which discuss add-ons for it. In this case the authors discuss car stereo add-ons which allow the user to control the ipod from the unit itself. Too bad they all seem to be >$100.