When we need to find a specified file in thousands of files, we may be able to use LS filename. The results will list some information about this file. But this is not the best way. Or specify the rules of the file, LS obviously can not meet our needs. In either case, we need a strong assistant-grep.
GREP also has several variants, namely Egrep, Fgrep, Rgrep. In these variants, I only know that Egrep is grep-e. That is, the regular variant of grep.
grep [option] "pattern" [filename]
The reason here is to enclose the file name in brackets is because the file name is optional. grep can get data from standard input.
option has a number of options. Because there are too many options, the man manual also deliberately makes a classification of these options. Look what it is:
1. Matching mode selection
-E Regular Match
None of the others have been used.
2. Matching control
-I ignores case
-E can specify multiple pattern
-f Specifies the pattern file, that is, you can place the field you want to find in a file, which is more commonly used when specifying multiple pattern.
-V Reverse lookup. That is, lists the mismatches.
3. Basic Output control
-C Prints the number of matching rows, not the number of times
--color= ... In general, this value is auto. Specify the color of the output
-L Print mismatched files
-L Print matching files
-O Prints matching pattern only
-Q silent mode, need to pass $? To check the return value to determine if a match is found
4. Output line Decoration
-B Print the first letter offset, note that this cheap is not the offset of the match character, if you want to print the offset of the matching character need to match the-o option
-h/h The former does not have the file name printing, the latter with the file name printing. default Print file name
The first line of the-n print file matches the pattern
-Z Sometimes, a file uses a space as a delimiter, and the space is the IFS of the shell environment, so there is a possibility of splitting a filename into two file names. In the face of this problem, the solution is-Z, which specifies the delimiter as a string.
5. Selection of files and folders
-A to treat all files as text files
--exclude= ... Removes the specified file
The file name that needs to be ignored is listed in--exclude-from=file file.
--exlucde-dir= ... Ignore the specified folder
OK, that's all. It's a headache to remember all this. I almost remember it anyway.
There are also three more fun options-a-b-C. The meaning is, print matches the first few lines, print matching after a few lines, print match before and after each line. A and so on followed by a number.
Let's take a look at some of the uses of grep.
Filter the file under SH as suffix in the current folder, and then filter the expect file in the sh file. And hit the front and back lines. Then test the-O and-B options.
Locate the file that contains the bash string in the current directory. There is a lot of is a directory to eliminate it.
Print only the matching file names, then print the mismatched file names, and then look at the-h/h effect. Print out the matching information in the first few lines
The full catalog search excludes all files in a directory. Here's why it's not a bug. List the differences between my two commands.
Two times the difference between the inputs is one with one or the other. The result is not the same. It seems to be remembered later, Harvest Ah.
Regular expressions are no longer introduced, you can write a few articles about it. There are also many resources on the Internet. Students can refer to it. (In fact, I am also a regular expression of the two knife)
OK. Let's introduce so much. Every time when using grep feel to know less, suddenly a summary found, know a lot of ah. In fact, the key is to use the above, shell programming is the case, there may be some commands we do not know, but can be done according to the command we know.
9 Shell command grep