What is the equivalent of the grep command in Windows?

Asked

Viewed 25,651 times

20

Under Linux, when I want to filter by a term when a command will generate giant list, I use the command grep.

For example:

 ls | grep "termo"

However, in Windows there is no command grep.

What would be the equivalent of grep on Windows, for both Power Shell and CMD?

  • 4

    Powershell: https://superuser.com/a/701011; cmd: https://superuser.com/a/300821

  • You can use findstr. It would look like this: "dir | findstr String"

  • 2

    I have a better idea, gets only Linux!!! :D

  • 1

    Install the bash!

3 answers

35


You can use two options in Windows:

Using the Command Prompt: Findstr

Examples:

Find files containing the expression: log: dir /B | findstr /R /C:"[log]":

inserir a descrição da imagem aqui

where:

  • dir /B: Lists files/directories from the current directory.
  • findstr /R /C: Accepts regular expressions and looks for a literal string, respectively.


Search between the contents of the file(s) and the expression(s): log: findstr log *:

inserir a descrição da imagem aqui


Using the Powershell: Select-String

Example:

Find files containing the expression: log: Get-ChildItem *log*:

inserir a descrição da imagem aqui

Search between the contents of the file(s) and the expression(s): log: Get-ChildItem | Select-String -Pattern "log":

inserir a descrição da imagem aqui

  • What is the function of /C?

  • /C is when there is a need to search for a string in its literal form, example: if you want to search for the words: log ativo, use the findstr /C:"log ativo" *, but, if you need to search for the words: log ativo, separately, do not need to inform the /C: findstr "log ativo" *.

10

I believe you’re looking for findstrat cmd

Example:

C:> dir /B | findstr /R /C:"[mp]"

In Powershell is the sls:

PS C:> New-Alias sls Select-String

-1

The grep command currently works on Windows 10, does the test, may already be available in its version.

Example:

cat package.json | grep react-native

Browser other questions tagged

You are not signed in. Login or sign up in order to post.