?

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:
[User Picture]From: portlandpiglet
2008-01-29 06:54 pm (UTC)
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.

Can I just say: That's pretty hot. The whole "I'm a genius who answers come to in my sleep and I get so excited about them that I have to jump up and write them down immediately in my bathrobe." Call me a nerd, but that's sexy.

(Reply) (Thread)
[User Picture]From: jofish22
2008-01-29 07:33 pm (UTC)
what i also love about little shell scripts is that some whiney little prat is sure to post about how you could do it better in less lines/characters/time or using an alternate p* language.
(Reply) (Thread)
From: chaosqueen42
2008-01-29 11:20 pm (UTC)
Awh, that means I don't get to post:

perl? meh. sed!

For extra credit, do it in DOS....
(Reply) (Parent) (Thread)
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)
[User Picture]From: madbodger
2008-01-30 02:21 am (UTC)
Dude. So true. I'm always dashing off these quickie scripts to do stuff that sometimes boggles the fulltime sysadmins. Nothing like | to encourage (compiled) code re-use!

(Reply) (Thread)
[User Picture]From: ocschwar
2008-01-30 03:58 am (UTC)
Is this a NetCDF 3 versus 2 issue?

And why do I remember this 7 bloody years after I last had to deal with it?
(Reply) (Thread)
[User Picture]From: dr_tectonic
2008-01-30 05:09 am (UTC)
No, it's just modelers writing code that spits out "Mean_Temperature" instead of "Mean Temperature" for no particularly good reason.

We're actually close to NetCDF-4 at this point, so if anybody's still writing 2-style output, they're waaaay behind the times.
(Reply) (Parent) (Thread)