如何计算PowerShell中的行数

Chr*_*nch 8 powershell

我有一个文本文件,我需要在PowerShell中阅读.

每行中的最后一个字符是Y或N.

我需要浏览文件并输出有多少Y和N在那里.

有任何想法吗?

Aar*_*ron 16

假设一个名为"test.txt"的文件...要获得以Y结尾的行数,您可以这样做:

get-content test.txt | select-string Y$ | measure-object -line
Run Code Online (Sandbox Code Playgroud)

要获得以N结尾的行数,您可以这样做:

get-content test.txt | select-string N$ | measure-object -line
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 对于任何想要在变量中获得此答案的google,这是[code] $ a =(Get-Content filename.txt | Select-String"| Y $"| Measure-Object).count [/ code] (7认同)

man*_*lds 7

要在一行中获得两个计数:

gc .\test.txt | %{ if($_ -match "Y$|N$"){ $matches[0]} } | group
Run Code Online (Sandbox Code Playgroud)