Tuesday, June 23, 2009

TIMTOWTDI: There Is More Than One Way To Do It

My favorite programming language is still the shell.

Hal Pomeranz, whom I finally met at the OS Bridge conference in Portland last week, has a blog called Command-line Kung Fu full of shell tricks and tips. He posts to it on Tuesdays.

In today's, a post on how to decide what tools to use and when, he recommends cut(1) for some tasks because "cut is the only tool that allows you to easily pull out ranges of characters."

Ah. A challenge.

Well, you can do his example:

grep -l spammer@example.com qf* | cut -c3- | xargs -I'{}' rm qf{} df{}
without cut at all.

grep -l spammer@example.com qf* | while read i; do echo ${i:2}; done | ...
I confess I'd do it differently, but that's because I like substitution better than cuts.

for f in $(grep -l spammer@example.com qf*); do rm $f ${f/q/d}; done
It's pronounced "Tim-toady." Hal emailed me saying he still likes his way better. YMMV.

For jobs like this, where I'm using a program to write commands on-the-fly, I usually play it safe. I start off using echo to see what the shell thinks I want to do

for f in $(grep -l spammer@example.com qf*); do echo rm $f ${f/q/d}; done
which spits out a list of the generated commands.

rm qf137965 df137965
rm qf200920 df200920
Then, when I think it's right, instead of removing the echo -- which risks a typo -- I recall the command, and tack on a pipe to a subshell, which executes the commands.

for f in $(grep -l spammer@example.com qf*); do echo rm $f ${f/q/d}; done | bash

No comments: