url address
1.12) grep
Linux下面查找文本命令grep, 类似于Window编辑器的ctrl+F查找我们想要的内容,
PS:对比learning skill
看一下它的基础用法,准备一个目录文件和文本文件,
打印出这个单词,或者包含有这个字母的所有字符串
[root@iZ2vc5lqzt23aweti4j777Z ~]# grep hello April29th2024/testfile1.txt
hello
helloworld
会发现,它是区分大小写的>>如果忽略大小写, -i (ignore),
-w表示精确匹配, (The “-w” option in grep is used to match the search pattern as a whole word, )
反向查找,
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep hello testfile1.txt
hello
helloworld
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -v hello testfile1.txt
Hello
Helloworld in the testfile1.txt
Today is a sunny day
(The -v option instructs grep to print all lines that do not contain or match the expression. The –v option tells grep to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don’t match the expression.)
一个目录下面所有包含这个字符的文件
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -r hello /root/April29th2024
/root/April29th2024/testfile1.txt:hello
/root/April29th2024/testfile1.txt:helloworld
/root/April29th2024/testfile3.txt:hello
/root/April29th2024/testfile3.txt:helloworld
/root/April29th2024/testfile2.txt:hello
/root/April29th2024/testfile2.txt:helloworld in the testfile1.txt
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -r hello
testfile1.txt:hello
testfile1.txt:helloworld
testfile3.txt:hello
testfile3.txt:helloworld
testfile2.txt:hello
testfile2.txt:helloworld in the testfile1.txt
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]#
-r 当前文件夹下面包含字符的文件行,-lr, 查找文件夹和子文件夹下面,包含字符串的文件
grep -lr Explained: The grep -lr command searches for a specific pattern in all files within a directory and its subdirectories. It then lists the filenames of the files that contain the pattern. The -l option tells grep to only print the filenames, and the -r option tells it to search recursively through all subdirectories.
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -r hello
testfile1.txt:hello
testfile1.txt:helloworld
testfile3.txt:hello
testfile3.txt:helloworld
testfile2.txt:hello
testfile2.txt:helloworld in the testfile1.txt
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -lr hello
testfile1.txt
subdirectory/testfile1.txt
testfile3.txt
testfile2.txt
-E 启用regularly expression, | means or
[root@iZ2vc5lqzt23aweti4j777Z April29th2024]# grep -E ‘hello|today’ testfile1.txt
hello
helloworld
,TDL
: to make search efficiency higher, we can use grep command with regular expression together,
{The abbreviation of “-r” in grep is “recursive.” This means that the grep command will search for the specified pattern in all files within the specified directory and its subdirectories.
📝 Other common grep abbreviations:
-i: Case-insensitive search
-v: Invert the match, showing lines that don’t match the pattern
-w: Match whole words only
-n: Show line numbers
-c: Show only the count of matching lines
-l: Show only the names of files with matching lines}