gpt4 book ai didi

powershell - 相当于 “head -n-3”的PowerShell?

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

我已经能够追踪基本的头部/尾部功能:

head -10 myfile <==> cat myfile | select -first 10
tail -10 myfile <==> cat myfile | select -last 10

但是,如果我要列出除最后三行之外的所有行或除前三行之外的所有行,您该怎么做?在Unix中,我可以执行“head -n-3”或“tail -n + 4”。对于PowerShell应该如何完成尚不清楚。

最佳答案

像-First和-Last参数一样,还有一个-Skip参数会有所帮助。值得注意的是,-Skip基于1,而不是零。

# this will skip the first three lines of the text file
cat myfile | select -skip 3

我不确定PowerShell是否会为您提供除预构建的最后n行之外的所有内容。如果知道长度,则可以从行数中减去n并使用select中的-First参数。您还可以使用仅在填充时使行通过的缓冲区。
function Skip-Last {
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][PsObject]$InputObject,
[Parameter(Mandatory=$true)][int]$Count
)

begin {
$buf = New-Object 'System.Collections.Generic.Queue[string]'
}

process {
if ($buf.Count -eq $Count) { $buf.Dequeue() }
$buf.Enqueue($InputObject)
}
}

作为演示:
# this would display the entire file except the last five lines
cat myfile | Skip-Last -count 5

关于powershell - 相当于 “head -n-3”的PowerShell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079572/

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