Grep and grep commands
Grep (global search regular expression (RE) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows. Unix grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in a regular expression represent the literal meaning of the regular expression. They are no longer special. Linux uses GNU grep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.
Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.
Grep can be used in shell scripts because grep returns a status value to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.
2. grep Regular Expression metacharacter set (basic set)
^
For example, '^ grep' matches all rows starting with grep.
The end of the anchor row is as follows: 'grep 'Matches all rows ending with grep.
.
Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character and then p.
*
Match zero or multiple previous characters, for example :'Grep 'matches all one or more spaces followed by grep rows. .Represents any character.
[]
Matches a character in a specified range, for example, '[Gg] rep' matches Grep and grep.
[^]
Match a character that is not within the specified range, such as '[^ A-FH-Z] rep' match a letter that does not start with the A-R and T-Z, followed by the rep line.
(..)
Mark matching characters, such as '(love)'. love is marked as 1.
\ <
Anchor specifies the start of a word, for example :'\