首页 > 解决方案 > 如何在 PowerShell 中删除行范围

问题描述

我正在尝试删除多个文件范围。Mu文件包含1000行,我想在PowerShell中像这样删除第70-500行,第600-100行,

我试过-replace了,但它不起作用。

尝试将行索引值 9 替换为 10:

(cat test.txt) -replace (cat test.txt)[9..10],"" | sc test.txt -Force

标签: powershellreplace

解决方案


使用ReadCount属性Get-Content添加到输出:

Get-Content 'test.txt' | Where-Object {
    -not ($_.ReadCount -ge 70 -and $_.ReadCount -le 500) -and
    -not ($_.ReadCount -ge 600 -and $_.ReadCount -le 1000)
}

推荐阅读