如何在没有搜索猴子的情况下像在 Windows 中一样在文件中搜索字符串?

use*_*056 67 command-line vmware search virtual-console

我在 VMWare 上使用 Ubuntu,但由于安全限制我无法连接到互联网。

我想知道是否有办法通过终端搜索字符串并找到字符串位于文件中的哪一行。

Pan*_*her 101

几乎有太多的选择要列出

grep -r 'pattern_to_match' directory_to_search
Run Code Online (Sandbox Code Playgroud)

将输出与模式匹配的文件名和完整行。


xxb*_*nxx 30

我使用的最好的是带有选项 -ri 的 grep 命令(递归和不区分大小写的搜索):

$ grep -r <text_pattern_to_search> directory_or_path_to_search
Run Code Online (Sandbox Code Playgroud)

可能对您有用的选项:

    -i - case insensitive
    -r, --recursive  like --directories=recurse
    -R, --dereference-recursive  likewise, but follow all symlinks
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
Run Code Online (Sandbox Code Playgroud)

有关详细信息,您可以执行grep --helpman grep在 linux 终端中执行。

干杯


αғs*_*нιη 9


如果您只想找到文件中 sting 所在的行号,请使用以下命令:

grep -n '/string_To_Find/=' directory/file_Name
Run Code Online (Sandbox Code Playgroud)

如果要查找行号并输出字符串所在行的完整行名,请使用以下命令:

grep -n 'string_To_Find' directory/file_Name
Run Code Online (Sandbox Code Playgroud)

如果您只想找到字符串所在的完整行名称,请使用以下命令:

grep -r 'string_To_Find' directory/file_Name
Run Code Online (Sandbox Code Playgroud)