Topics


Blogs


Forums


Samples


Media


Labs


Resources

 




DevCentral > Weblogs > Joe Pruitt - A Software Architect's take on Network Security
 Unix To PowerShell – Head
posted on Tuesday, April 14, 2009 9:52 AM

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.



 
      

Feedback


6/17/2009 7:52 AM
Gravatar Wow Joe,
Thanks so much for posting these PowerShell versions of old favorite Unix commands.
Any change you might post up the script files for the earlier entries like you did for the later?
Much thanks!
Scott
Scott
 Leave Feedback
Title  
Name  
Email
Url
Comments   
Please add 2 and 5 and type the answer here: