Powershell删除行数不正确的行

问题描述 投票:0回答:1

这可以轻松地完成还是完全在Powershell中完成?如何从“ test.txt”中删除所有不完全包含24个单词的行

powershell count
1个回答
0
投票

在PowerShell中这不太难。

如下所示的方法:

# read the file and use Where-Object to capture only those lines that have 24 words exactly.
# the regex -split uses '\W+', meaning to split each line on (at least one) Non-Word character.
$result = Get-Content -Path 'D:\test.txt' | Where-Object { ($_ -split '\W+').Count -eq 24 }

# output on screen
$result

# write output to new file
$result | Out-File -FilePath 'D:\test24.txt' -Force
© www.soinside.com 2019 - 2024. All rights reserved.