output filename, not string with select-string

前端 未结 3 1105
栀梦
栀梦 2021-02-01 05:04

I\'m using powershell to \"grep\" my source code for a particular string. If the string is in the file, I would like the name of the file, not the line of code that contains th

3条回答
  •  一个人的身影
    2021-02-01 05:25

    When I'm doing this I just use the -List parameter - yes it does display the line of code but you only get one line per file (no matter how many matches there are):

    PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list
    
    Commands\SnapinHelp\CmdletInfo.cs:27:        public List InputTypes;
    Commands\SnapinHelp\GetSnapinHelpCommand.cs:124:            WriteXmlNodeList(c...
    Commands\SnapinHelp\ParameterInfo.cs:73:        XmlNode FindNode(XmlDocument doc)
    Commands\Xml\XmlCommandBase.cs:65:            RegisterInputType(Proce...
    

    If you want the path:

    PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list | 
        Format-Table Path
    
    Path
    --------
    C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\CmdletInfo.cs
    C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\GetSnapinHelpCommand.cs
    C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\ParameterInfo.cs
    C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\Xml\XmlCommandBase.cs
    

    Or if you really only want the filename:

    PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list | 
        Format-Table Filename
    
    Filename
    --------
    CmdletInfo.cs
    GetSnapinHelpCommand.cs
    ParameterInfo.cs
    XmlCommandBase.cs
    

提交回复
热议问题