8.14 "Linux learning is not difficult" Linux Common Operations Command (+): grep command finds a string in a file that matches a condition
Use grep command can finds a string within a file that matches a condition.
Command syntax:
grep [ options ] [ Find mode ] [ file ]
The meanings of the options in the command are as shown in the table.
Options |
option meaning |
- e |
Pattern is an extensible regular expression |
- F |
A pattern is a set of fixed-length strings separated by a break character |
- P |
Pattern is a Perl regular expression |
- b |
Displays the byte offset in the file of the line containing the matching string before each line of the output |
- C |
Show only the number of matching rows |
- I. |
Comparison is case insensitive |
- L |
Show only matching file names |
- L |
Show only filenames that don't match |
- N |
Add the line number of the line that matches the string before the output (the first line of the file is the line number 1) |
- v |
Show only lines that do not contain matching characters |
- E < Mode > |
Using patterns to perform matching operations |
- F < file > |
Get the pattern from the file |
- R |
Recursively, read all files in each directory |
Example: in a file KKK match characters in search "Test File" .
[[email protected] ~]# grep ' test file ' KKK test file
Example: in a file AA all include at least 5 the row data contents of a contiguous lowercase character.
[[email protected] ~]# grep ' [a-z]\{5\} ' AA aaaaa aaaaaa
Example: in /ROOT/AA in the file Find out the b the line content at the beginning.
[[email protected] ~]# grep ^b/root/aa BBB
Example: in /ROOT/AA in the file the output is not b the line content at the beginning.
[Email protected] ~]# grep-v ^b/root/aa aaaaa aaaaa BBB aaaaaa
Example: in the /ROOT/KKK in the file output to le the line content at the end.
[[email protected] ~]# grep le$/root/kkk test file
This article extracts from "Linux Learning is not difficult" textbook, which is one of the most practical, theoretical and practicalLinuxIntroductory materials. To read the entire book, please visitLinuxoperation and Maintenance person(www.linuxywz.com) website.
"Linux learning is not difficult" Linux Common Operations Command (+): grep command to find a string in a file that matches a condition