Your basic ITPro blog... What's going on at work, what I'm interested in.

Wednesday, August 27, 2008

Programming vs. Scripting

The famed 'Powershell Guy' visited again... and again I am blown away. I posted about working on text parsing and included my script to solve the problem. My buddy did basically the same task in Perl in just a couple lines of code. I thought that was cool. I couldn't even imagine a way of doing it in PoSH with that little amount of code.

But then, I'm not MoW... He drops the following in the comments:

gc iptest.txt |? {$_ -match 'REMOTE_ADDR: (\S+)'} |% {$matches[1]} | group | sort count

Very, very cool! (BTW, the second one-liner you posted errors out with a 'Cannot index into a null array' message. Odd.)

This prompted a great discussion (after a few moments marveling over the one-liner) about the differences between the one-liner and the script. I think in scripts... assign values to variables; compute against those variables, assigning the results in more variables; if-then's and loops; etc. It is never my first inclination to start with a one-liner. I realize that this is in large part to my lack of background at the command-line (and newness to Powershell and scripting in general).

I want to be able to do this. The pipeline (the heart of the one-liner) is very powerful. I need to learn how to use this tool. There is a time for a structured, formal 'solution' and there is a time when you just 'need it'. MoW's one-liner answers the question and gives me what I need. For the one-off request, this is the way to go. If this turns out to be analysis that I am doing on a regular basis, a script will be more appropriate; formatting output, reusable, etc.

Episodes like this make me more frustrated and more excited about my job than ever!   :-) I love this stuff!

No comments:

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332