Welcome!

A Software Architect Living in a Networking World

Joe Pruitt

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


Blog Feed Post

Unix To PowerShell – Head

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.

head

The Unix head command will print the first 10 lines of each file to standard output.  With more than one file, it will precede each with a header giving the file name.  This command is useful when you just want to quickly scan the first few lines of a collection of files.  PowerShell has the equivalent Get-Content Cmdlet but it only allows you to specify the first “n” lines, it does not include support for querying the first “n” bytes like the unix “head” command does.  So, I’ve added some code around the Get-Content cmdlet to allow for querying the first “n” number of bytes as well.

   1: #----------------------------------------------------------------
   2: # Head.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,
   7:   [int]$num_bytes = -1,
   8:   [int]$num_lines = -1,
   9:   [bool]$quiet = $false
  10: );
  11:  
  12: function Do-Head()
  13: {
  14:   param
  15:   (
  16:     [string]$filespec = $null,
  17:     [int]$num_bytes = -1,
  18:     [int]$num_lines = -1
  19:   );
  20:   
  21:   # if no bytes or lines specified, default to 10 lines
  22:   if ( (-1 -eq $num_bytes) -and (-1 -eq $num_lines) ) { $num_lines = 10; }
  23:   
  24:   [System.IO.FileInfo[]]$files = Get-ChildItem $filespec;
  25:   foreach ($file in $files)
  26:   {
  27:     $cur_bytes = 0;
  28:     if ( ($files.Length -gt 1) -and ($false -eq $quiet) )
  29:     {
  30:       "";
  31:       "==> $($file.Name) <==";
  32:     }
  33:  
  34:     [string[]]$lines = Get-Content $file -TotalCount $num_lines;
  35:     for ($i=0; $i -lt $lines.Length; $i++)
  36:     {
  37:       $line = $lines[$i];
  38:       if ( -1 -ne $num_lines )
  39:       {
  40:         # -1 means all lines
  41:         $line;
  42:       }
  43:       elseif ( -1 -ne $num_bytes )
  44:       {
  45:         if ( ($cur_bytes + $line.Length) -le $num_bytes )
  46:         {
  47:           $line;
  48:           $cur_bytes += $line.Length;
  49:         }
  50:         else
  51:         {
  52:           $line.SubString(0, $num_bytes - $cur_bytes);
  53:           $cur_bytes = $num_bytes;
  54:           break;
  55:         }
  56:       }
  57:     }
  58:   }
  59: }
  60:  
  61:  
  62: Do-Head -filespec $filespec -num_bytes $num_bytes -num_lines $num_lines -quiet $quiet

The script is fairly straightforward.  First Get-ChildItem is called on the included filespec and the resulting FileInfo array is iterated for each file in the result set.  The total lines requested defaults to 10 if it is not supplied to go along with the functionality of the UNIX head command.  Then Get-Content is called with the requested number of lines (-1 = all).  Each line is iterated upon and either printed out in whole, or truncated if it ends the specified byte range.

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.