Appearance
也可以使用 BurntSushi/ripgrep
从文件中查找字符串
-n显示行号-R递归从目录查找-i不区分大小写-w按照单词查找,比如查找 sys,不会去匹配 system
bash
$ rg -nw sys /etc/passwd
4:sys:x:3:3:sys:/dev:/usr/sbin/nologin
$ rg -nw root /etc/passwd
1:root:x:0:0:root:/root:/bin/bashgrep 是一种 Linux/Unix 系统下常用的命令行工具,用于在文件或者文本数据中进行关键字搜索,并输出符合条件的行。下面是 grep 命令的简要概述:
bash
grep [options] [pattern] [files]其中,[options] 为可选项,用于指定搜索时的选项参数;[pattern] 为要搜索的模式或关键词;[files] 为要搜索的文件列表,可以使用通配符指定多个文件。
grep 命令的常用选项包括:
-i:搜索时不区分大小写;-r:递归搜索子目录中的文件;-v:输出不匹配的行;-n:同时输出行号;-c:只输出匹配的行数;-A:输出匹配行以及之后的N行;-B:输出匹配行以及之前的N行;-C:输出匹配行以及之前和之后的N行。
例如,要在文件 example.txt 中查找包含关键字 hello 的行,可以使用以下命令:
bash
grep "hello" example.txt如果要进行大小写不敏感的搜索,可以加上 -i 选项:
bash
grep -i "hello" example.txt如果想要同时输出行号,可以加上 -n 选项:
bash
grep -n "hello" example.txt如果要递归搜索当前目录下的所有 .txt 文件,可以使用以下命令:
bash
grep -r "hello" *.txt