PowerShell: how to grep command output?

前端 未结 7 1326
温柔的废话
温柔的废话 2021-01-30 08:27

In PowerShell I have tried:

alias | select-string Alias

This fails even though Alias is clearly in the output. I know this is beca

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 08:44

    Your problem is that alias emits a stream of AliasInfo objects, rather than a stream of strings. This does what I think you want.

    alias | out-string -stream | select-string Alias
    

    or as a function

    function grep {
      $input | out-string -stream | select-string $args
    }
    
    alias | grep Alias
    

    When you don't handle things that are in the pipeline (like when you just ran 'alias'), the shell knows to use the ToString() method on each object (or use the output formats specified in the ETS info).

提交回复
热议问题