1. grep Introduction 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. $ For example, '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, such as '* grep'. Match 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. /< Specify the start of a word, for example, '//> Anchor specifies the end of a word. For example, 'grep/> 'matches the row containing the word ending with grep. X/{M /} Repeat the characters X and M, for example, '0/{5/} 'matches the rows that contain 5 o. X/{M ,/} Repeat character X for at least m times, for example, 'o/{5,/} 'matches rows with at least 5 o. X/{M, N /} Repeated characters X, at least m times, no more than N times. For example, 'o/{5, 10/} 'matches rows of 5-10 o. /W Match text and number characters, that is, [A-Za-z0-9], such as: 'G/W * P' match with g followed by zero or multiple characters or numbers, followed by P. /W /W reverse form, matching one or more non-word characters, such as periods and periods. /B The word lock, for example, '/bgrepb/' matches only grep. 3. Meta character extension set for egrep and grep-e + Matches one or more previous characters. For example, '[A-Z] + able' matches one or more lower-case letters followed by able strings, such as loveable, enable, and disable. ? Matches zero or multiple previous characters. For example, 'gr? P' matches gr followed by one or no characters, and then the row of P. A | B | C Match A, B, or C. For example, grep | sed matches grep or sed. () Group symbols, such as: Love (able | RS) ov + matches loveable or lovers and matches one or more ov. X {m}, X {M,}, X {m, n} Same role as X/{M/}, X/{M,/}, X/{M, N /} 4. POSIX character class POSIX (the Portable Operating System Interface) adds a special character class, for example [: alnum:] is another way of writing a A-Za-z0-9 to preserve one character encoding in different countries. Put them in the [] sign to become a regular expression, such as [A-Za-z0-9] or [[: alnum:]. In Linux, grep supports POSIX character classes except fgrep. [: Alnum:] Character [: Alpha:] Character [: Digit:] Numeric characters [: Graph:] Non-empty characters (non-space and control characters) [: Lower:] Lowercase characters [: Cntrl:] Control characters [: Print:] Non-empty characters (including spaces) [: Punct:] Punctuation Marks [: Space:] All blank characters (new line, space, Tab) [: Upper:] Uppercase characters [: Xdigit:] Hexadecimal number (0-9, A-F, A-F) 5. grep Command Options -? At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower rows of matching rows. -B, -- byte-offset Print the block number of the row before the matching row. -C, -- count Only the number of matched rows is printed, and the matching content is not displayed. -F file, -- file = File Extract templates from files. The empty file contains 0 templates, so nothing matches. -H, -- no-filename When multiple files are searched, the matching file name prefix is not displayed. -I, -- ignore-case Ignore case differences. -Q, -- quiet Cancel display. Only the exit status is returned. 0 indicates that the matched row is found. -L, -- files-with-matches Print the list of files matching the template. -L, -- files-without-match Print the list of files that do not match the template. -N, -- line-Number Print the row number before the matched row. -S, -- silent The error message about the nonexistent or unreadable file is not displayed. -V, -- revert-match Reverse search: Only unmatched rows are displayed. -W, -- word-Regexp If it is referenced by/<and/>, the expression is used as a word search. -V, -- version Displays the software version information. 6. Instance To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression. $ LS-L | grep '^' Filter the LS-L output content in the MPs queue and display only the rows starting with. $ Grep 'test' D * Display all the lines containing test in files starting with D. $ Grep 'test' AA BB CC The row Matching Test is displayed in the AA, BB, and CC files. $ Grep '[A-Z]/{5/} 'aa Display All rows of a string that contains at least five consecutive lowercase characters. $ Grep 'W/(ES/) T. */1 'aa If West is matched, ES is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another ES (/1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape the "/" number and write it as 'W (ES) T. */1. |