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

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
5/26/2010 8:08 PM
Gravatar Thanks for this great script buddy. All the best.
Payday Loan Lenders
12/31/2011 6:19 AM
Gravatar Thanks for this great info, I always enjoy reading your posts! thanks 5**
wonag promo code
1/1/2012 5:38 AM
Gravatar I really enjoy your posts! thanks for a great read!
mini loans

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 4 and 8 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