PowerShell equivalent to grep -f

前端 未结 9 1656
情书的邮戳
情书的邮戳 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"
    

提交回复
热议问题