gpt4 book ai didi

powershell - 如何从Powershell中的文本表中删除第n列?

转载 作者:行者123 更新时间:2023-12-02 23:07:19 31 4
gpt4 key购买 nike

假设我正在使用格式良好的表。以kubectl输出为例:

NAME            READY   STATUS    RESTARTS   AGE     IP          NODE   NOMINATED NODE   READINESS GATES
me-pod-name 2/2 Running 0 6s 10.0.0.10 node1 <none> <none>
me-pod-name-2 1/1 Running 0 6d18h 10.0.1.20 node2 <none> <none>
me-pod-name-3 1/1 Running 0 11d 10.0.0.30 node3 <none> <none>

我倾向于观看此类输出并记录很多更改。在这种情况下,我想从表中删除中间列之一,但仍然得到不错的输出。例如。让我们尝试删除AGE列,因为它发生了很大变化,并且在资源年轻时对观看无用:
NAME            READY   STATUS    RESTARTS   IP          NODE   NOMINATED NODE   READINESS GATES
me-pod-name 2/2 Running 0 10.0.0.10 node1 <none> <none>
me-pod-name-2 1/1 Running 0 10.0.1.20 node2 <none> <none>
me-pod-name-3 1/1 Running 0 10.0.0.30 node3 <none> <none>

我的问题是: 如何轻松删除此类列并仍然输出格式正确的表,而所有其他列均完好无损? 列的大小并不总是相同(例如age并不总是8个字符宽)。我想找到一些可重用的单线解决方案,因为我经常使用CLI工具(不仅涉及k8),还需要对其进行过滤。另外,我想避免基于模式的解决方案,因为我需要为要删除的每一列生成正则表达式-我能够做到这一点,但这需要我为每个用例编写特定的解决方案。

我尝试使用ConvertFrom-String和format表,但是这与数据格式非常混乱(例如,“1/1”被视为日期格式,这种情况不正确)。

最佳答案

您正在查看的是固定宽度数据。
为了解决这个问题,Import-Csv不会这样做,所以我前段时间做了几个函数来将固定宽度的数据转换为固定宽度的数据。

function ConvertFrom-FixedWidth {
[CmdletBinding()]
[OutputType([PSObject[]])]
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string[]]$Data,

[int[]]$ColumnWidths = $null, # an array of integers containing the width in characters for each field
[string[]]$Header = $null, # if given, a string array containing the columnheaders
[switch]$AllowRaggedContent # if set, the function accepts the last items to be trimmed.
)

# If the data is sent through the pipeline, use $input to collect is as array
if ($PSCmdlet.MyInvocation.ExpectingInput) { $Data = @($Input) }
# or use : $Data = $Input | ForEach-Object { $_ }

if (!$ColumnWidths) {
Write-Verbose "Calculating column widths using first row"
# Try and determine the width of each field from the top (header) line.
# This can only work correctly if the fields in that line do not contain space
# characters OR if each field is separated from the next by more than 1 space.

# temporarily replace single spaces in the header row with underscore
$row1 = $Data[0] -replace '(\S+) (\S+)', '$1_$2'

# Get the starting index of each field and add the total length for the last field
$indices = @(([regex] '\S+').Matches($row1) | ForEach-Object {$_.Index}) + $row1.Length
# Calculate individual field widths from their index
$ColumnWidths = (0..($indices.Count -2)) | ForEach-Object { $indices[$_ + 1] - $indices[$_] }
}

# Combine the field widths integer array into a regex string like '^(.{10})(.{50})(.{12})'
$values = $ColumnWidths | ForEach-Object { "(.{$_})" }
if ($AllowRaggedContent) {
# account for fields that are too short (possibly by trimming trailing spaces)
# set the last item to be '(.*)$' to capture any characters left in the string.
$values[-1] = '(.*)$'
}
$regex = '^{0}' -f ($values -join '')

Write-Verbose "Splitting fields and generating output"
# Execute a scriptblock to convert each line in the array.
$csv = & {
switch -Regex ($Data) {
$regex {
# Join what the capture groups matched with a comma and wrap the fields
# between double-quotes. Double-quotes inside a fields value must be doubled.
($matches[1..($matches.Count - 1)] | ForEach-Object { '"{0}"' -f ($_.Trim() -replace '"', '""') }) -join ','
}
}
}
if ($Header) { $csv | ConvertFrom-Csv -Header $Header }
else { $csv | ConvertFrom-Csv }
}

function ConvertTo-FixedWidth {
[CmdletBinding()]
[OutputType([String[]])]
Param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[PSObject[]]$Data,

[Parameter(Mandatory = $false)]
[ValidateRange(1, 8)]
[int]$Gap = 2
)

# if the data is sent through the pipeline, use $input to collect is as array
if ($PSCmdlet.MyInvocation.ExpectingInput) { $Data = @($Input) }
# or use : $Data = $Input | ForEach-Object { $_ }

# get the headers from the first object in the correct order
Write-Verbose "Retrieving column headers"
$headers = $Data[0].PSObject.Properties | ForEach-Object {$_.Name }

# calculate the maximum width for each column
Write-Verbose "Calculating column widths"
$columnWidths = @{}
foreach ($item in $Data) {
foreach ($column in $headers) {
$length = [Math]::Max($item.$column.Length, $column.Length)
if ($column -ne $headers[-1]) { $length += $Gap }
if ($columnWidths[$column]) { $length = [Math]::Max($columnWidths[$column], $length) }
$columnWidths[$column] = $length
}
}

# output the headers, all left-aligned
$line = foreach ($column in $headers) {
"{0, -$($columnWidths[$column])}" -f $column
}
# output the first (header) line
$line -join ''

# regex to test for numeric values
$reNumeric = '^[+-]?\s*(?:\d{1,3}(?:(,?)\d{3})?(?:\1\d{3})*(\.\d*)?|\.\d+)$'

# next go through all data lines and output formatted rows
foreach ($item in $Data) {
$line = foreach ($column in $headers) {
$padding = $columnWidths[$column]
# if the value is numeric, align right, otherwise align left
if ($item.$column -match $reNumeric) {
$padding -= $Gap
"{0, $padding}{1}" -f $item.$column, (' ' * $Gap)
}
else {
"{0, -$padding}" -f $item.$column
}
}
# output the line
$line -join ''
}
}

使用示例将这些功能设置到位,您可以执行以下操作:
# get the fixed-width data from file and convert it to an array of PSObjects 
$data = (Get-Content -Path 'D:\Test.txt') | ConvertFrom-FixedWidth -AllowRaggedContent -Verbose

# now you can remove any column like this
$data = $data | Select-Object * -ExcludeProperty 'AGE'

# show on screen
$data | Format-Table -AutoSize

# save to disk as new fixed-width file
$data | ConvertTo-FixedWidth -Gap 3 -Verbose | Set-Content -Path 'D:\Output.txt'

# or you can save it as regular CSV to disk
$data | Export-Csv -Path 'D:\Output.csv' -NoTypeInformation

屏幕结果:

NAME          READY STATUS  RESTARTS IP        NODE  NOMINATED NODE READINESS GATES
---- ----- ------ -------- -- ---- -------------- ---------------
me-pod-name 2/2 Running 0 10.0.0.10 node1 <none> <none>
me-pod-name-2 1/1 Running 0 10.0.1.20 node2 <none> <none>
me-pod-name-3 1/1 Running 0 10.0.0.30 node3 <none> <none>


结果保存到定宽文件中:

NAME            READY   STATUS    RESTARTS   IP          NODE    NOMINATED NODE   READINESS GATES
me-pod-name 2/2 Running 0 10.0.0.10 node1 <none> <none>
me-pod-name-2 1/1 Running 0 10.0.1.20 node2 <none> <none>
me-pod-name-3 1/1 Running 0 10.0.0.30 node3 <none> <none>

关于powershell - 如何从Powershell中的文本表中删除第n列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541667/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com