Linux basic command grep

grep

Objective: To use the grep command to find the file in qualifying string
format:
grep [options] [search pattern] [filename]

Take a look at an example:
create a file test1.txt, the content of the file is:
aaa
AAAAAA
abc
abcabcabc
cbacbacba
match_pattern
nand-> erase

First of all, when looking for a string, we want to display the following:
1) the name of the file where it is located --- grep already displays the target file name by default when searching for
2) the line number in the file where it is located ------ use- n option

grep -rn "string" file name
r (recursive): recursive search
n (number): display the line number of the target position
string: is the target string
to be found File name: the target file to be found, if it is * Means to find all files and directories under the current directory

For example:
grep -n "abc" test1.txt Find the string abc in test1.txt
grep -rn "abc" * Recursively search for the string abc in the current directory abc
grep -rnw "abc" * Recursively match the search character in the current directory String abc

book@www.100ask.org:~/linux/dira$ grep -n "abc" test1.txt
3:abc
4:abcabcabc
book@www.100ask.org:~/linux/dira$ grep -rn "abc" *
dirb/test1.txt:3:abc
dirb/test1.txt:4:abcabcabc
test1.txt:3:abc
test1.txt:4:abcabcabc
book@www.100ask.org:~/linux/dira$ grep -rnw "abc" *
dirb/test1.txt:3:abc
test1.txt:3:abc

Published 53 original articles · praised 16 · visits 2213

Guess you like

Origin blog.csdn.net/m0_37757533/article/details/105351573