PowerShell versus Korn Shell example

As I dig deeper into PowerShell from an Administrator perspective, I try to use it as much as possible in my daily work as a true shell. In my opinion, you open up a shell at the beginning of the day and basically ‘live it in’. You do most of your work from it, and slowly surround yourself with quick little functions and tools. You then can create tiny ‘throw-away’ scripts over and over again to solve whatever small problem you face during the day. This has been my approach using the korn and bash unix shells and I think it is a great starting point for PS. This is very different from programming in a language like C# or even VB.

A shell seeks to solve very different problems from traditional compiled languages and can even help you to figure out what you are trying to figure out if you don’t initially know. Working in a shell can be a liberating and elegantly simply REPL experience. This is a stark contrast to using graphical interfaces to solve problems. While initial data extraction may be easier and faster, all of the pieces of your solution need to be reinvented and rewritten for a new application or tool and data must be extracted from one and placed into the next. Even if you could have a swiss-army knife graphical tool (this is probably excel), you still are limited to how its authors imagined that you could use it. By writing new scripts or functions or simply downloading new tools from the internet, the power of a shell is endless.

Since most of the problems I have to solve with a shell are unix problems, it might seem that powershell couldn’t help me. This isn’t true. Because both have a strong base of using the pipe ( | ) to cascade text from one command to another, I simply pipe my unix problem into my powershell and solve it in Windows rather than AIX. In this way I can get PowerShell ‘under my fingers’ so I can turn to it quickly.

In this case, I need to pull out MAC addresses from an error report, get rid of duplicates, and email the results to some one else, with one result per line. I have a cool command that can get what my initial error reporting command returns from unix into powershell on the fly, but for this comparison, my data always starts in a file called ‘errpt.txt’.

The data in the file mostly looks like this over and over again:

----------------------------------------------------
LABEL:          AIXIF_ARP_DUP_ADDR
Date/Time:       Fri Jun 19 22:41:54 CDT 2009
Node Id:         nortameraix14
Class:           S
Type:            PERM

Detail Data
DUPLICATE IP ADDRESS
0534 0622
MAC ADDRESS
0015 1763 E07B   <<<---- This is the line I need
----------------------------------------------------

In Korn Shell, I would run:

cat errpt.txt | grep "^00"
(which would return)
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
...

Since I noticed that every line I needed started with '00', I just piped into a unix grep command and matched to the regular expression for "line starts with 00". At this point I turn to my PowerShell pocket reference, which is sadly useless. Next I simply go to google and type 'powershell grep' and one of the top examples tells me about a command in powershell called select-string. So instead of wading through a big tutorial about powershell, I just do a strategic strike and find someone else's blog where they already make the comparison for me:

PS> type errpt.txt | select-string "^00"
(which would return)
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
...

So the commands were almost identical:

unix: cat errpt.txt | grep "^00" 
PS:   type errpt.txt  | select-string "^00"

The only problem with my example is that in order to learn about 'select-string' I had to know to tell google that it needed to work like 'grep'. If I didn't know that I wouldn't have been able to leverage the knowledge of other unix guys who made the switch. All I can say is that once you learn about 'select-string', you have to potiential to type 'select-string unix' into google and you would learn about grep if you ever needed to. Maybe in ten years you will type 'select-string fooshell' into some search engine to learn about the next hot thing that would be similar to what is now 'select-string' in PowerShell.

At any rate, the next thing I need to do is show each unique line only one time, in unix the command is 'sort -u' which sorts the line first and then removes duplicate lines. I can just bring up my old command, go to the end of the line, add another pipe, and then put my sort in place:

cat errpt.txt | grep "^00" | sort -u
000C CAC4 8E4E
0014 2135 807C
0015 1763 DE95
0015 1763 E07B
0015 1763 E3AB
0015 C5FF 49D2

This is basically all I need to do. I now have a list of unique MAC addresses, one to a line.
In powershell, I now just have to understand how to do a 'sort -u', hit up-arrow to bring back my last command, and add it in.

I eventually find that I have to put two commands together. Here are the results sided by side:

ksh:   cat errpt.txt | grep "^00" | sort -u
PS:  type errpt.txt | select-string "^00" | sort | get-unique

So you might say that Korn shell wins since I have to type a little less to get to my result. I am going to give PowerShell a free pass this time because I know that its main goal in life is to manipulate rich .NET data in ways that Korn shell can't and that is where is ends its similarity with unix and really shines.

Leave a Reply

Your email address will not be published. Required fields are marked *