Topics


Blogs


Forums


Samples


Media


Labs


Resources

 




DevCentral > Weblogs > Joe Pruitt - A Software Architect's take on Network Security
 Unix To PowerShell - Cat
posted on Friday, May 01, 2009 9:00 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.

cat

The Unix “cat” command is used to concatenate and display files.   Given a file or filename list, it will print the contents of that file to standard output.  There are several options in the Unix command that are implemented with the following PowerShell arguments:

Unix PowerShell Description
-b filespec A file or file matching pattern to concatenate.
-n -number Prefix all output lines with line numbers
-b -number_nonblank Prefix nonblank output lines with numbers.
-E -show_ends display “$” at the end of each line.
-T -show_tabs Display TAB characters as “^I”
-s -squeeze_blanks Never more than one consecutive single blank line
 
   1: #----------------------------------------------------------------
   2: # Cat.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,
   7:   [bool]$number = $false,  # number output lines
   8:   [bool]$number_nonblank = $false, # Number nonblank output lines
   9:   [bool]$show_ends = $false, # Display $ at end of each line
  10:   [bool]$squeeze_blank = $false, # never more than one single blank line
  11:   [bool]$show_tabs = $false # display TAB characters as ^I
  12: );
  13:  
  14: #----------------------------------------------------------------
  15: # function Do-Cat
  16: #----------------------------------------------------------------
  17: function Do-Cat()
  18: {
  19:   param
  20:   (
  21:     [string]$filespec = $null,
  22:     [bool]$number = $false,  # number output lines
  23:     [bool]$number_nonblank = $false, # Number nonblank output lines
  24:     [bool]$show_ends = $false, # Display $ at end of each line
  25:     [bool]$squeeze_blank = $false, # never more than one single blank line
  26:     [bool]$show_tabs = $false # display TAB characters as ^I
  27:   );
  28:  
  29:   if ( $number_nonblank ) { $number = $false; }
  30:  
  31:   if ( $filespec )
  32:   {
  33:     $files = @(Get-ChildItem $filespec -ErrorAction SilentlyContinue);
  34:     if ( $files.Length –gt 0 )
  35:     {
  36:       $cur_number = 1;
  37:       foreach ($file in $files)
  38:       {
  39:         $content = @(Get-Content -Path $file);
  40:         $last_line = $null;
  41:         
  42:         # iterate through the lines in the file
  43:         for ($i=0; $i -lt $content.Length; $i++)
  44:         {
  45:           $line = $content[$i];
  46:           
  47:           # If sqeezing blanks, if the this and last lines are empty, 
  48:           # continue to next line
  49:           if ( $squeeze_blank )
  50:           {
  51:             if ( ($last_line -ne $null) -and ($last_line.Length -eq 0) -and ($line.length -eq 0) )
  52:             {
  53:               continue;
  54:             }
  55:           }
  56:           
  57:           # Append "$" to ends of lines
  58:           if ( $show_ends )
  59:           {
  60:             $line = $line + "$";
  61:           }
  62:           
  63:           # Replace tabs with "^I"
  64:           if ( $show_tabs )
  65:           {
  66:             $line = $line.Replace("`t", "^I");
  67:           }
  68:           
  69:           # Prefix with numbers.  number_nonblank overrides number
  70:           if ( $number_nonblank )
  71:           {
  72:             if ( $line.Length -gt 0 )
  73:             {
  74:               $line = "{0,6}  {1}" -f $cur_number, $line;
  75:               $cur_number++;
  76:             }
  77:           }
  78:           elseif ( $number )
  79:           {
  80:               $line = "{0,6} {1}" -f $cur_number, $line;
  81:               $cur_number++;
  82:           }
  83:           
  84:           # Keep last line for next pass
  85:           $last_line = $content[$i];
  86:           
  87:           $line;
  88:         }
  89:         
  90:       }
  91:     }
  92:     else
  93:     {
  94:       "No files matching pattern '$filespec' found!";
  95:     }
  96:   }
  97: }
  98:  
  99: Do-Cat -filespec $filespec -number $number -number_nonblank $number_nonblank `
 100:   -show_ends $show_ends -squeeze_blank $squeeze_blank -show_tabs $show_tabs;

You can download the full script here: Cat.ps1



 
      

Feedback


8/15/2009 8:26 AM
Gravatar useful script, thanks
will

9/2/2009 2:38 PM
Gravatar Great little script - cheers!
Acai Berry
 Leave Feedback
Title  
Name  
Email
Url
Comments   
Please add 5 and 7 and type the answer here: