A Software Architect Living in a Networking World

Joe Pruitt

Subscribe to Joe Pruitt: eMailAlertsEmail Alerts
Get Joe Pruitt: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Twitter on Ulitzer, New Media on Ulitzer

Blog Feed Post

Unix To PowerShell - Wc

PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

wc

The Unix “wc” (word count) command will print the character, word, and newline counts for each file specified and a total line if more than one file is specified.  This command is useful for quickly scanning a directory for small and large files or to quickly look at a file and determine it’s relative size.  The Get-Content Cmdlet will return the number of characters in the full but not the number of lines and words.  The following script will emulate the behavior of the Unix “wc” command with a few changes in the way parameters are supplied.

   1: #----------------------------------------------------------------
   2: # Wc.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,
   7:   [bool]$lines = $false,
   8:   [bool]$words = $false,
   9:   [bool]$chars = $false,
  10:   [bool]$longest = $false
  11: );
  12:  
  13: #----------------------------------------------------------------
  14: # function New-Stat
  15: #----------------------------------------------------------------
  16: function New-Stat()
  17: {
  18:   param([string]$name);
  19:   $stat = "" | select Lines, Words, Chars, Longest, Name;
  20:   $stat.Lines = $stat.Words = $stat.Chars = $stat.Longest = 0;
  21:   $stat.Name = $name;
  22:   $stat;
  23: }
  24:  
  25: #----------------------------------------------------------------
  26: # function Get-ColumnList
  27: #----------------------------------------------------------------
  28: function Get-ColumnList
  29: {
  30:   param
  31:   (
  32:     [bool]$lines = $false,
  33:     [bool]$words = $false,
  34:     [bool]$chars = $false,
  35:     [bool]$longest = $false
  36:   );
  37:   $cols = $null;
  38:   if ( $lines ) { $cols += @("Lines"); }
  39:   if ( $words ) { $cols += @("Words"); }
  40:   if ( $chars ) { $cols += @("Chars"); }
  41:   if ( $longest ) { $cols += @("Longest"); }
  42:   $cols += @("Name");
  43:   
  44:   $cols;
  45: }
  46:  
  47: #----------------------------------------------------------------
  48: # function Do-Wc
  49: #----------------------------------------------------------------
  50: function Do-Wc()
  51: {
  52:   param
  53:   (
  54:     [string]$filespec = $null,
  55:     [bool]$lines = $false,
  56:     [bool]$words = $false,
  57:     [bool]$chars = $false,
  58:     [bool]$longest = $false
  59:   );
  60:   
  61:   # if no options are given, set them all to true
  62:   if ( !$lines -and !$words -and !$chars -and !$longest )
  63:   {
  64:     $lines = $words = $chars = $longest = $true;
  65:   }
  66:   
  67:   $stats = $null;
  68:   
  69:   $stat_total = New-Stat -name "total";
  70:   
  71:   [System.IO.FileInfo[]]$files = Get-ChildItem $filespec -ErrorAction SilentlyContinue;
  72:   if ( $null -ne $files )
  73:   {
  74:     foreach ($file in $files)
  75:     {
  76:       # new stat object for current file
  77:       $stat = New-Stat -name $file.Name;
  78:       
  79:       [string[]]$content = Get-Content $file;
  80:       
  81:       # wc only prints newlines so the last line is omitted
  82:       $stat.Lines = $content.Length -1;
  83:       
  84:       # iterate through the lines in the file
  85:       for ($i=0; $i -lt $content.Length; $i++)
  86:       {
  87:         $info = $content[$i] | Measure-Object -word -line -char
  88:         $stat.Words += $info.Words;
  89:         $stat.Chars += $info.Characters;
  90:         if ( $info.Characters -gt $stat.Longest ) { $stat.Longest = $info.Characters }
  91:         
  92:         # increment the count for newline characters (except for last line)
  93:         if ( $i -lt ($content.Length-1) ) { $stat.Chars += 2; }
  94:       }
  95:       
  96:       $stat_total.Chars += $stat.Chars;
  97:       $stat_total.Lines += $stat.Lines;
  98:       $stat_total.Words += $stat.Words;
  99:       
 100:       $stats += @($stat);
 101:     }
 102:     $stats += @($stat_total);
 103:  
 104:     $stats | select (Get-ColumnList -lines $lines -words $words -chars $chars -longest $longest) | ft -AutoSize;
 105:   }
 106:   else
 107:   {
 108:     "No files matching pattern '$filespec' found!";
 109:   }
 110: }
 111:  
 112: Do-Wc -filespec $filespec -lines $lines -words $words -chars $chars -longest $longest;

*Update – Thanks to twitter user @Jaykus for the tips on the Measure-Object cmdlet.  Although it didn’t offer a complete replacement, it did make the script faster than the character iteration for each line.

*Update 2 – Added support for longest line count as well.

Read the original blog entry...

More Stories By Joe Pruitt

Joe Pruitt is a Principal Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.