【问题标题】:Select-String command returns filename, line number and text but I only want the textSelect-String 命令返回文件名、行号和文本,但我只想要文本
【发布时间】:2016-04-11 15:34:29
【问题描述】:
@powershell -command sls "succeeded" pol_inst.log 

给我

pol_inst.log:259: Security Gateway policy installation succeeded for:

我不想

pol_inst.log:259:

我似乎找不到简单的答案

【问题讨论】:

  • “@”这个东西应该做什么?
  • @JaquelineVanek 这是命令提示符的回显字符。他从批处理文件或cmd.exe 调用。

标签: powershell


【解决方案1】:
Select-String "succeeded" pol_inst.log | ForEach-Object Line

或者从命令行调用它:

@powershell -command Select-String "succeeded" pol_inst.log ^| ForEach-Object Line

【讨论】:

    【解决方案2】:

    发生这种情况是因为Select-String 向您返回了MatchInfo 对象的集合。除了结果之外,这些还包含很多东西。

    简单的解决方案是像这样使用.Line 属性,

    select-string -path c:\myFile -pattern "myPattern" | % { $_.line }
    

    【讨论】:

      【解决方案3】:

      扩展Select-String 产生的MatchInfo 对象的Line 属性:

      Select-String ... | Select-Object -Expand Line
      

      【讨论】:

        猜你喜欢
        • 2011-02-11
        • 2011-02-12
        • 1970-01-01
        • 2011-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多