【问题标题】:How do I return only the integer from Measure-Object in PowerShell?如何仅从 PowerShell 中的 Measure-Object 返回整数?
【发布时间】:2019-08-04 14:49:17
【问题描述】:

我想运行一段代码来计算文本文件中有多少个字符并将其另存为另一个文本文件,但我需要输出只是一个数字。

这是我在 PowerShell 中运行的代码:

Get-Content [File location] | Measure-Object -Character | Out-File -FilePath [Output Location]

它会像这样保存输出:

Lines Words Characters Property
----- ----- ---------- --------
                     1         

有没有办法只保存号码?

【问题讨论】:

    标签: powershell measure-object


    【解决方案1】:

    基本的PowerShell:

    (Get-Content file | Measure-Object -Character).characters
    

    Get-Content file | Measure-Object -Character | select -expand characters
    

    相关:How to get an object's property's value by property name?

    【讨论】:

      【解决方案2】:

      Anything 是 PowerShell 中的一个对象,它也适用于 Measure-Object 的结果。要仅获取某个属性的值,请使用Select-Object -ExpandProperty <PropertyName>获取所需的属性值;

      PS> Get-ChildItem | Measure-Object | Select-Object -ExpandProperty Count
      PS> 3
      

      在你的例子中:

      PS> Get-Content [File location] | 
          Measure-Object | 
          Select-Object -ExpandProperty Count | 
          Out-File -FilePath [Output Location]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        • 2019-11-12
        • 1970-01-01
        • 2012-01-12
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多