#---------------------------------------------------------------- # Cat.ps1 #---------------------------------------------------------------- param ( [string]$filespec = $null, [bool]$number = $false, # number output lines [bool]$number_nonblank = $false, # Number nonblank output lines [bool]$show_ends = $false, # Display $ at end of each line [bool]$squeeze_blank = $false, # never more than one single blank line [bool]$show_tabs = $false # display TAB characters as ^I ); #---------------------------------------------------------------- # function Do-Cat #---------------------------------------------------------------- function Do-Cat() { param ( [string]$filespec = $null, [bool]$number = $false, # number output lines [bool]$number_nonblank = $false, # Number nonblank output lines [bool]$show_ends = $false, # Display $ at end of each line [bool]$squeeze_blank = $false, # never more than one single blank line [bool]$show_tabs = $false # display TAB characters as ^I ); if ( $number_nonblank ) { $number = $false; } $files = @(Get-ChildItem $filespec -ErrorAction SilentlyContinue); if ( $files.Length -gt 0 ) { foreach ($file in $files) { $content = @(Get-Content -Path $file); $cur_number = 1; $last_line = $null; # iterate through the lines in the file for ($i=0; $i -lt $content.Length; $i++) { $line = $content[$i]; # If sqeezing blanks, if the this and last lines are empty, # continue to next line if ( $squeeze_blank ) { if ( ($last_line -ne $null) -and ($last_line.Length -eq 0) -and ($line.length -eq 0) ) { continue; } } # Append "$" to ends of lines if ( $show_ends ) { $line = $line + "$"; } # Replace tabs with "^I" if ( $show_tabs ) { $line = $line.Replace("`t", "^I"); } # Prefix with numbers. number_nonblank overrides number if ( $number_nonblank ) { if ( $line.Length -gt 0 ) { $line = "{0,6} $line" -f $cur_number; $cur_number++; } } elseif ( $number ) { $line = "{0,6} $line" -f $cur_number; $cur_number++; } # Keep last line for next pass $last_line = $content[$i]; $line; } } } else { "No files matching pattern '$filespec' found!"; } } Do-Cat -filespec $filespec -number $number -number_nonblank $number_nonblank ` -show_ends $show_ends -squeeze_blank $squeeze_blank -show_tabs $show_tabs;