gpt4 book ai didi

powershell - 在 Powershell 中提取数组的最后一行

转载 作者:行者123 更新时间:2023-12-02 03:07:32 25 4
gpt4 key购买 nike

我正在使用 Powershell 通过 API 提取数据。出于某种原因,输出提供了多行数据,但我只对最后一行感兴趣。

如何修剪或过滤数组,以便它只显示行,例如{“预览”:假​​...

在我得到最后一行之后,我将使用 ConvertFrom-Json 将其转移到哈希表中

如有任何建议,我们将不胜感激!

$output
{"preview":true,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1152091","TotalUnsuccessful":"13759","Total":"1165850","Unsuccessful%":"6.80"}}
{"preview":true,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1204330","TotalUnsuccessful":"14265","Total":"1218595","Unsuccessful%":"6.76"}}
{"preview":true,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1233151","TotalUnsuccessful":"14500","Total":"1247651","Unsuccessful%":"6.75"}}
{"preview":true,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1249102","TotalUnsuccessful":"14714","Total":"1263816","Unsuccessful%":"6.76"}}
{"preview":true,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1262812","TotalUnsuccessful":"14794","Total":"1277606","Unsuccessful%":"6.73"}}
{"preview":false,"offset":0,"lastrow":true,"result":{"TotalSuccess":"1271004","TotalUnsuccessful":"14873","Total":"1285877","Unsuccessful%":"6.73"}}

最佳答案

多个选项,假设 $output 是一个数组或类似的可枚举集合:

# using `Select-Object`:
$output |Select -Last1

# using array index
$output[$output.Length - 1]

# using negative array index
$output[-1]

# iterate over collection until you reach the end:
$last = $null
foreach($item in $output){
$last = $item
}

如果 $output 是一个多行字符串,使用 -split 正则表达式运算符首先在换行符上拆分:

$output -split '\r?\n' |Select -Last 1

关于powershell - 在 Powershell 中提取数组的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560634/

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