本帖最后由 newswan 于 2024-7-21 19:56 编辑
支持全角字符
format-table.ps1 "in.txt" "out.txt"- param(
- [String]$filenameIn ,
- [String]$filenameOut
- )
-
- $file = Get-Content -Path $filenameIn -Encoding UTF8
- $maxWidth = @{}
-
- $file | ForEach-Object {
- $arr = $_ -split "\s+"
- for ( $i = 0 ; $i -lt $arr.count ; $i++ ){
- $Width = ($arr[$i]).length + ( $arr[$i] -replace "[\x00-\x7F]","" ).length
- if ( $maxWidth[$i] -lt $Width ) {
- $maxWidth[$i] = $Width
- }
- }
- }
-
- $strTable = [System.Collections.ArrayList]@()
-
- $file | ForEach-Object {
- $arr = $_ -split "\s+"
- $str = ""
- for ( $i = 0 ; $i -lt $arr.count ; $i++ ){
- $LenHZ = ( $arr[$i] -replace "[\x00-\x7F]","" ).length
- $str += ($arr[$i]).PadRight( $maxWidth[$i] - $LenHZ , " " ) + ":"
- }
- [void]$strTable.add( $str ) #$str -replace "\s$",""
- }
- $strTable | Out-File -Encoding 'UTF8' -FilePath $filenameOut
复制代码
|