Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

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
9/1/2011 11:25 AM
Gravatar Crazy to code this. Built-ins seem easier like select.
cat file.txt | select -first 10
Julio Vega
11/22/2011 7:25 AM
Gravatar While Julio's one line version is elegant and wonderful, the caveat is that you're instructing Powershell to pipe through the entire contents of the file to select. It'll work, but if you're dealing with GB worth of text logs, you'll be in for quite a wait.

It would be nice if Powershell had some forward-analysis capability to avoid doing throwaway work these kinds of situations.
ERock

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 7 and 5 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or