"Reprint" Grep,egrep,fgrep detailed

Source: Internet
Author: User

"Reprinted from" http://blog.csdn.net/homking/article/details/6000711

Egrep equivalent to Grep-e

Fgrep equivalent to Grep-f

GREP-E xxx-e yyy can specify multiple conditions and can contain-

Grep-a 3 print matching line and match line first 3 lines

Grep-b 3 print matching lines and 3 rows after matching lines

Grep-c 3 print matching lines and 3 rows before and after matching rows

grep--color=auto Match highlighting to add alias in ~/.BASHRC

This set of commands searches for a file in a specified pattern and informs the user of what file it is searching for in a string that matches the specified pattern, and prints out all lines of text that contain the string, preceded by the file name of the line. The grep command can only search for one specified pattern at a time; The EGREP command retrieves an extended regular expression (including expression groups and optional); The FGREP command retrieves a fixed string that does not recognize a regular expression, which is a quick search command.

This set of commands is useful for searching and locating specific topics in a file. the patterns to be searched can be considered as keywords, and you can use them to search for these keywords contained in the file. When you write a program, you can use it to find a function, or a related phrase. The grep command is more powerful than fgrep because the search pattern of the grep command can be a regular expression, while fgrep cannot.

Each command in the group command has a set of options that can be used to change the way it is exported. For example, you can add line numbers to a line of text that you have searched for, or output only line numbers for lines of text, or output all lines of text that do not match the search pattern, or simply output a file name that has been searched for a specified pattern, and you can specify that the case be ignored when looking for mode.

This set of commands looks for rows in the specified input file that match the pattern. If no file is specified, it is read from the standard input. Normally, each matching row is displayed to the standard output. If the file you are looking for is more than one, add the file name before each line output.

Grammar:

grep [Options] [Find mode] [file name 1, file name 2, ...]

egrep [Options] [Find mode] [file name 1, file name 2, ...]

fgrep [Options] [Find mode] [file name 1, file name 2, ...]

The options for this command have the following meanings:

-E Each pattern is treated as an extended regular expression.
-F Each pattern is treated as a set of fixed strings (separated by new lines) and not as regular expressions.
-B Displays the byte offset in the file for the row containing the matching string before each line of the output.
-C displays only the number of matching rows.
-I comparisons are case insensitive.
-H when locating multiple files, instructs grep not to add filenames to the output.
-L Displays the name of the first match string and separates it with a newline character.
This file name is not displayed repeatedly when a matching string appears in a file more than once.
-n precede the output with the line number of the row that matches the string (the first line of the file is the line number 1).
-V displays only rows that do not contain a matching string.
-X displays only rows that match exactly the entire row.
-e expression specifies the mode in which the retrieval is used. Used to prevent patterns that begin with "-" from being interpreted as command options.
-F Expfile Gets the pattern to search from the Expfile file, one row for the pattern.

The use of this group of commands should also be noted in the following areas:

Type the search mode after the command, and then type the file you want to search for. In the file name list, special characters such as "*" can be used to generate a list of file names. If you want to include a string in the search pattern with spaces, you can enclose the pattern you want to search in single quotation marks to indicate that the pattern of the search consists of a string containing spaces. Otherwise, the Shell will consider the space as a delimiter for the command-line arguments, and the grep command will interpret the words in the search pattern as part of the list of file names. In the following example, the grep command searches the file example for the pattern "text file".

grep ' text file ' example

The user can use the shell special characters on the command line to generate the list of file names to be searched. In the following example, the special character "*" is used to generate a list of file names that contain all the files in the current directory. This command will search all the files in the current directory that match the pattern.

grep data *

Special characters are useful when searching for a specified set of files. For example, if you want to search for a specific pattern in all C program source files, you can use "*.c" to specify the list of file names. Assuming that the user's C program contains some unnecessary steering statements (goto statements), to find these statements, you can search for and display all lines of code that contain goto statements using the following command:

grep Goto *.C

Users can type the search mode on the command line, or they can use the-f option to read the pattern to be searched from the specified file. In the file, each search pattern occupies one row. This is useful if you often want to search for a common set of strings. In the following example, the user searches for the string "editor" and "create" in the file exam, places the pattern to be searched in the file mypats, and then the grep command reads the pattern to be searched from the file mypats.

GREP-F mypats Exam

"Reprint" Grep,egrep,fgrep detailed

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.