8.14 "Learning Linux is not difficult" Linux common operation commands (14): grep command to find qualified strings in a file
Use the grep command to find matching strings within a file. Command syntax:
grep [ options ] [ find pattern ] [ files ]
The meaning of each option in the command is shown in the table.
Options |
Meaning of options |
-E |
pattern is an extensible regular expression |
-F |
A pattern is a set of fixed-length strings separated by line breaks |
-P |
pattern is a Perl regular expression |
-b |
Display the byte offset in the file of the line containing the matching string before each line of output |
-c |
Show only the number of matching lines |
-i |
case-insensitive comparison |
-l |
Show only matching filenames |
-L |
Only show filenames that don't match |
-n |
Add the line number of the line where the matched string is located before the output (the line number of the first line of the file is 1) |
-v |
Show only lines that do not contain matching characters |
-e < mode > |
Use patterns for matching operations |
-f < file > |
get schema from file |
-r |
Recursively, read all files in each directory |
Example: Search for matching characters "test file" in file kkk .
[root@rhel ~]# grep 'test file' kkk test file
Example: Display all line data content that contains at least 5 consecutive lowercase characters in file aa .
[root @ rhel ~] # grep '[az] {5 \}' aa aaaaa aaaaaa
Example: Find the content of the line starting with b in the /root/aa file .
[root@rhel ~]# grep ^b /root/aa bbb
Example: Output the line content that does not start with b in the /root/aa file .
[root@rhel ~]# grep -v ^b /root/aa aaaaa AAAAA BBB aaaaaa
Example: Output lines ending with le in the /root/kkk file .
[root@rhel ~]# grep le$ /root/kkk test file
This article is excerpted from " Learning Linux is not difficult"Teaching material, this is a most practical Linux introductory teaching material with equal emphasis on theory and practice. To read the entire book, please visit Linux Operators ( www.linuxywz.com) website .