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 - Fold

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.

fold

The Unix “fold” command will take as input a list of files and wrap each input line to fit within a specified width.  By default, fold breaks lines wider than 80 columns and the output is split into as many lines as necessary.

I’ve included the following runtime arguments in my implementation:

Unix PowerShell Description
-s -spaces Break at spaces when possible.
-w -width Use WIDTH columns instead of 80.

The code is fairly straightforward.  Each file is iterated upon and then for each file, each line is processed.  if the width is less than or equal to the specified width, it is printed to the console.  If it is greater than the width, if “spaces” is not specified, it is broken in chunks of WIDTH characters.  If “spaces” is specified, then if the character at the WIDTH marker is not a space character and the following character is not either, a reverse search is performed back to the beginning of the line to search for the first space character.  If one is found, the line up to that point is sent to the output.

   1: #----------------------------------------------------------------
   2: # Fold.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,
   7:   [bool]$spaces = $false,
   8:   [int]$width = 80
   9: );
  10:  
  11: #----------------------------------------------------------------
  12: # function Is-SpaceChar
  13: #----------------------------------------------------------------
  14: function Is-SpaceChar()
  15: {
  16:   param([char]$c);
  17:   $isSpace = ([string]$c).Trim().Length -eq 0;
  18:   $isSpace;
  19: }
  20:  
  21: #----------------------------------------------------------------
  22: # function Do-Fold
  23: #----------------------------------------------------------------
  24: function Do-Fold()
  25: {
  26:   param
  27:   (
  28:     [string]$filespec = $null,
  29:     [bool]$spaces = $false,
  30:     [int]$width = 80
  31:   );
  32:  
  33:   $files = @(Get-ChildItem $filespec -ErrorAction SilentlyContinue);
  34:   if ( $null -ne $files )
  35:   {
  36:     foreach ($file in $files)
  37:     {
  38:       $content = @(Get-Content $file);
  39:       if ( $content )
  40:       {
  41:         foreach ($line in $content)
  42:         {
  43:           # if line is greater than width, chunk it up
  44:           while ($line.Length -ge $width)
  45:           {
  46:             $widthtouse = $width;
  47:             if ( $spaces )
  48:             {
  49:               if ( Is-SpaceChar $line[$width-1] )
  50:               {
  51:                 # current break point is a space so use it.
  52:               }
  53:               elseif ( !(Is-SpaceChar $line[$width]) )
  54:               {
  55:                 # current char is not a space and neither
  56:                 # is the next one so we'll need to roll
  57:                 # back until we find a space.
  58:                 for($j=$width; $j -gt 0; $j--)
  59:                 {
  60:                   if ( Is-SpaceChar $line[$j] )
  61:                   {
  62:                     $widthtouse = $j;
  63:                     break;
  64:                   }
  65:                 }
  66:               }
  67:             }
  68:             
  69:             $subline = $line.SubString(0, $widthtouse);
  70:             $subline;
  71:             $line = $line.SubString($widthtouse);
  72:           }
  73:           
  74:           # output the remainder of the line
  75:           $line;
  76:         }
  77:       }
  78:     }
  79:   }
  80:   else
  81:   {
  82:     "No files matching pattern '$filespec' found!";
  83:   }
  84: }
  85:  
  86: Do-Fold -filespec $filespec -spaces $spaces -width $width;

*Known issues: The Unix version of fold measures output by columns, meaning special characters such as tabs and backspaces can account for different widths.  I’ve gone the easy route by using strings and string lengths which is analogous to the Unix “-b” option by default.

Download the full source here: Fold.ps1

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.