PowerShell equivalent to grep -f

前端 未结 9 1612
情书的邮戳
情书的邮戳 2020-12-22 16:09

I\'m looking for the PowerShell equivalent to grep --file=filename. If you don\'t know grep, filename is a text file where each line has a regular

相关标签:
9条回答
  • 2020-12-22 16:11

    I had the same issue trying to find text in files with powershell. I used the following - to stay as close to the Linux environment as possible.

    Hopefully this helps somebody:

    PowerShell:

    PS) new-alias grep findstr
    PS) ls -r *.txt | cat | grep "some random string"
    

    Explanation:

    ls       - lists all files
    -r       - recursively (in all files and folders and subfolders)
    *.txt    - only .txt files
    |        - pipe the (ls) results to next command (cat)
    cat      - show contents of files comming from (ls)
    |        - pipe the (cat) results to next command (grep)
    grep     - search contents from (cat) for "some random string" (alias to findstr)
    

    Yes, this works as well:

    PS) ls -r *.txt | cat | findstr "some random string"
    
    0 讨论(0)
  • 2020-12-22 16:12

    Maybe?

    [regex]$regex = (get-content <regex file> |
    foreach {
              '(?:{0})' -f $_
            }) -join '|'
    
    Get-Content <filespec> -ReadCount 10000 |
     foreach {
               if ($_ -match $regex)
                 {
                  $true
                  break
                 }
             }
    
    0 讨论(0)
  • 2020-12-22 16:16

    This question already has an answer, but I just want to add that in Windows there is Windows Subsystem for Linux WSL.

    So for example if you want to check if you have service named Elasicsearch that is in status running you can do something like the snippet below in powershell

    net start | grep Elasticsearch

    0 讨论(0)
  • 2020-12-22 16:20

    I'm not familiar with grep but with Select-String you can do:

    Get-ChildItem filename.txt | Select-String -Pattern <regexPattern>
    

    You can also do that with Get-Content:

    (Get-Content filename.txt) -match 'pattern'
    
    0 讨论(0)
  • 2020-12-22 16:26

    So I found a pretty good answer at this link: https://www.thomasmaurer.ch/2011/03/powershell-search-for-string-or-grep-for-powershell/

    But essentially it is:

    Select-String -Path "C:\file\Path\*.txt" -Pattern "^Enter REGEX Here$"
    

    This gives a directory file search (*or you can just specify a file) and a file-content search all in one line of PowerShell, very similar to grep. The output will be similar to:

    doc.txt:31: Enter REGEX Here
    HelloWorld.txt:13: Enter REGEX Here
    
    0 讨论(0)
  • 2020-12-22 16:29
    PS) new-alias grep findstr
    PS) C:\WINDOWS> ls | grep -I -N exe
    
    105:-a---        2006-11-02     13:34      49680 twunk_16.exe
    106:-a---        2006-11-02     13:34      31232 twunk_32.exe
    109:-a---        2006-09-18     23:43     256192 winhelp.exe
    110:-a---        2006-11-02     10:45       9216 winhlp32.exe
    
    PS) grep /?
    
    0 讨论(0)
提交回复
热议问题