“grep”不被识别为内部或外部命令

‘grep’ 不被识别为内部或外部命令

‘grep’ is not recognized as an internal or external command

grepWindows下使用命令时出现“’grep’不是内部或外部命令、可运行程序或批处理文件”的错误。

要解决此错误,请使用findstr命令搜索文件中的文本模式或使用 Git Bash。

grep 不被识别为内部或外部命令

findstr
命令是 Unix
命令
的 Windows 等价物
grep

下面是一个在名为
my_file.txt.

命令
findstr "app" my_file.txt

运行 findstr 命令窗口

findstr您可以在

文档的
这一部分查看其他使用示例。

或者,您可以通过在 Windows 计算机上grep安装来在 Git Bash 中运行该命令。git

如果你已经git安装了,你可以搜索Git Bash并使用
grep命令,否则,你必须git先安装。

grep在 Git Bash 中运行命令

要下载git并能够使用 Git Bash:

  1. 打开git 下载页面并下载适用于 Windows 的安装程序。
  2. 启动安装程序。
  3. 系统将提示您选择目标位置。您可以保留默认选项并单击Next

git 选择目标位置

  1. You will be prompted to select components on the next screen. Leave the
    default options and click Next.

git 选择组件

  1. Click Next on the screen that prompts you to “Select Start Menu Folder”.

  2. On the next screen, you can choose the default editor for Git, e.g.
    Notepad, Notepad++ or any other editor you prefer.

选择 git 默认编辑器

  1. On the “Adjust the name of the initial branch in new repositories screen”,
    click Next.

调整初始分支名称

  1. On the “Adjust your PATH environment” screen, make sure you have the default
    option of “Git from the command line and also from 3rd-party software”
    option selected and click “Next”.

调整路径环境

  1. For all the remaining screens, leave the default option selected and click
    Next.
  2. Lastly, click on the Install button to install git.

Once you have git installed, click on the Search field, type “Git Bash”
and start the application.

搜索 git bash

Now you can use the grep command directly in Git Bash.

GitBash
# 👇️ search for "apple" in the file `my_file.txt` grep "apple" my_file.txt # 👇️ search for "apple" recursively in all directories grep -r "apple" *

在 git bash 中使用 grep 命令

If you need to open Git Bash in a specific folder:

  1. Open the folder in a new window.
  2. Right-click in Explorer.

在文件夹中打开 git bash

  1. Click “Git Bash Here”.

Now you can use the grep command to search for patterns of text in files.

GitBash
# 👇️ search for "apple" in the file `my_file.txt` grep "apple" my_file.txt # 👇️ search for "apple" recursively in all directories grep -r "apple" *

# Additional Resources

You can learn more about the related topics by checking out the following
tutorials: