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;