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