?

Log in

No account? Create an account
Why CLI kicks ass - The Mad Schemes of Dr. Tectonic [entries|archive|friends|userinfo]
Beemer

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Why CLI kicks ass [Jan. 29th, 2008|10:48 am]
Beemer
[Tags|]

GUIs are awesome and powerful, and it's really cool how many things I can do in my browser anymore, but here's why the command-line interface and toolkit-style command utilities do and will continue to totally kick ass: because you can hook all these simple little independent modules together to do really complicated things.

foreach v ( `ls *nc | cut -f 1 -d _ | uniq` )
  foreach f ( $v*nc )
    set n = `ncdump -h $f | grep long_name | cut -f 2 -d '"' | grep _ | perl -pe 's/_/ /g'`
    ncatted -h -a long_name,$v,m,c,"$n" $f
  end
end


Isn't that horrible-looking? But it works. For those who don't speak shell, that means "Please go through all the NetCDF datafiles in this directory, find the 'long_name' attribute of the main variable in the metadata, and replace all the underscores in that tag with spaces. Because the modeler did it wrong."

It came to me this morning while I was waking up that it really was this easy to implement the fix, so I had to get up and write it all down while I was still in my bathrobe. It took longer to make sure I was doing it right before I ran it on the real data than it did to actually work out the details in the first place.

Go pipe! Go backticks!
LinkReply

Comments:
From: (Anonymous)
2008-01-29 11:22 pm (UTC)

So true

I mean, as long as you're invoking perl, why not just use that for the whole grep-cut-grep-perl sequence? Or, heck, the whole thing with just backticks for the ncdump an ncatted!
(Reply) (Parent) (Thread)
[User Picture]From: eto_theipi
2008-01-29 11:25 pm (UTC)

Re: So true

(I didn't mean this to be anonymous cowardice)
(Reply) (Parent) (Thread)
[User Picture]From: dr_tectonic
2008-01-30 12:08 am (UTC)

Re: So true

Oh, you know. The grep was sitting on top of the toollbox. If I wanted to do it all in perl, I would have had to dig through everything and find the ~= syntax connector, which always falls down to the bottom of the box and makes you hunt around for it. Plus I always put it in backwards on the first try.

And, I have to admit, I kinda like firing up the ol' cut. It just hums right along, splitting things into fields. I've even used it on image files. That sucker'll split anything. Watch your fingers!
(Reply) (Parent) (Thread)
[User Picture]From: eto_theipi
2008-01-30 03:10 am (UTC)

Re: So true

Just remember that it's a candle. If you turned it the other way you'd get burned!
(Reply) (Parent) (Thread)
[User Picture]From: dr_tectonic
2008-01-30 03:18 am (UTC)

Re: So true

That's... not actually helpful. Given that it's sideways no matter which way you do it!
(Reply) (Parent) (Thread)
[User Picture]From: eto_theipi
2008-01-30 03:20 am (UTC)

Re: So true

;D
(Reply) (Parent) (Thread)