Grep command details

Source: Internet
Author: User
Tags character classes egrep

Original article address:Http://bbs.chinaunix.net/thread-1687220-1-1.html

1. Introduction grep is a powerful text search tool that uses 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 to say, the metacharacters in the regular expression indicate that they return to their literal meaning and 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. All strings after the string template are searched 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. supported regular expression metacharacters (basic set) ^ start of anchor row, 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'. Match All one or more spaces followed by the grep line. . * 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, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by Rep. (..) Mark matching characters, such as '(love)', love is marked as 1. <Start of the anchor word, for example, '> end of the anchor word, for example, 'grep>' matches the row that contains the word ending with grep. The characters x \ {M \} are repeated for X and m times. For example, '0 \ {5 \} 'matches the rows containing 5 o. The characters x \ {M, \} are repeated at least m times, for example, 'O \ {5, \} 'matches rows with at least 5 o. The characters x \ {M, N \} are repeated at least m times, and must not be more than N times. For example, the line 'o \ {5, 10 \} 'matches 5--10 O. W matches text and numeric characters, that is, [A-Za-z0-9], for example, 'gw * P' matches with g followed by zero or multiple characters or numbers, followed by P. The reverse form of WW. It matches one or more non-word characters, such as periods and periods. B. For example, 'bgrepb' only matches grep. For more information about regular expressions, see 《 Regular Expression3. The 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 matches a, B, or C. For example, grep | sed matches grep or SED () grouping symbols, such as: Love (able | RS) ov + matches loveable or lovers and matches one or more ov. X {m}, X {M,}, X {m, n} act the same as X \ {M \}, X \ {M, \}, X \ {M, for more information about regular expressions, see 《 Regular Expression4. POSIX character classes in order to keep one character encoding in different countries, POSIX (the Portable Operating System Interface) adds special character classes, such as [: alnum:] is another way of writing A-Za-z0-9. Put them in the [] to become a regular expression, such as [-
Za-z0-9] or [[: alnum:]. In Linux, grep supports POSIX character classes except fgrep. [: Alnum:] character [: Alpha:] character [: digit:] digit character [: Graph:] non-null character (non-space, control character )[: lower:] lowercase character [: cntrl:] control character [: Print:] non-empty character (including space) [: punct:] punctuation [: Space:] all blank characters (new lines, spaces, tabs) [: Upper:] uppercase characters [: xdigit:] hexadecimal numbers (0-9, A-F, A-F) 5. grep Command Option -? At the same time, the number of rows matching the upper and lower rows is displayed, for example, grep-2 pattern.
Filename simultaneously displays the upper and lower two rows of matching rows. Note here? It does not mean a question mark. It means displaying the number of rows that match the upper and lower rows-B at the same time, -- byte-offset prints the position of the first character of the matching row in the entire file, that is, the number of characters starting from the file. -C, -- count: only the number of matched rows is printed, and the matching content is not displayed. -F file, -- file = file extract the string SEARCH template from the file. 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 is not displayed. Only the exit status is returned. 0 indicates that the matched row is found. -L, -- files-with-Matches: print the file list 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 does not display error messages about nonexistent or unreadable files. -V, -- revert-Match: Only unmatched rows are displayed. -W, -- word-Regexp if referenced by <and>, the expression is used as a word search. -V, -- version: displays the software version. 6. To use the grep tool for an instance, you must write a regular expression. Therefore, we will not explain all functions of grep as an example here. We will only list a few examples to illustrate how to write a regular expression. Ls-L | grep '^ a' filters the LS-L output content through MPs queues and only displays rows starting with. Grep 'test' D * displays all the lines containing test in files starting with D. Grep 'test' aa bb cc is displayed in the AA, BB, and CC files that match the test row. Grep '[A-Z] \ {5 \}' AA displays all rows of a string containing at least five consecutive lowercase characters. Grep 'W \ (ES \) T. * \ 1'
Aa if the 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 don't need to escape the "\" number, just write it as 'W (ES) T. * \ 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.