Use of regular matching in grep

Source: Internet
Author: User
To match the word "computer" or "computer", you can perform the following operations: [CC] mputer "." allows matching any characters in the ASCII set, letters, or numbers. The number of times that the matching letter A appears in the matching mode is \ {\} twice and ends with B. The operation is as follows: A \ {2 \} B matches a at least 4 times, if a \ {4, \} B is used as the number of occurrences, for example, a appears twice to 4 times: when a \ {2, 4 \} B inputs string parameters in the grep command, it is best to enclose them with double quotation marks. For example, "mystring ". There are two reasons for doing this: first, to prevent being misunderstood as a shell command, and second, to search for strings consisting of multiple words, such as "jet plane ", if double quotation marks are not used, plane is mistaken for a file and the query result returns the error message "file does not exist. When you enter a string parameter in the grep command, it is best to enclose it with double quotation marks. For example, "mystring ". There are two reasons for doing this: first, to prevent being misunderstood as a shell command, and second, to search for strings consisting of multiple words, such as "jet plane ", if double quotation marks are not used, plane is mistaken for a file and the query result returns the error message "file does not exist. When you call a variable, double quotation marks should also be used, such as: grep "$ myvar" file name. Otherwise, no results will be returned. Single quotation marks should be used for invocation mode matching. A more effective method for extracting exact matching using grep is to add \> after extracting strings. Assume that 48 is extracted accurately. The method is as follows: [???] # Grep "48 \>" data. F # grep-n "48 \>" data. F # display the row number. By default, grep is case-sensitive. to query strings that are case-insensitive, you must use the-I switch. When using a regular expression, it is best to enclose it in single quotes. Suppose you want to extract the location of the city where the code is 484 and 483, # grep '48 [34] 'data. f if you want to extract a record, make it not start with 48, # grep '^ [^ 48] 'data. f extracts all codes starting with L and ending with D with 5 characters. You can use the following method: # grep 'l... d 'data. f query all records whose names start with 5 and end with 1996 or 1998 # grep '5 .. 199 [68] 'data. f The first character is any number, the second character is between 0 and 5, and the third character is between 0 and 6: # grep '[0-9] [0-5] [0-6] 'data. F # grep '^ [0-9] [0-5] [0-6] 'data. F # extract all rows that contain numbers 4 at least twice at the beginning of the row: # grep '4 \ {2, \} 'data. the fgrep command adds the-e parameter, which allows the extension mode to match. For example, to extract the city code 219 or 216, use the following method: # grep-e '2017 | 100' data. F to use ^ and $ to query empty rows. Use the-n parameter to display the actual number of rows: # grep-n '^ $ 'data. f matches special character query characters with special meanings, such as $. '"* [] ^ | \ + ?, You must add \ before a specific character \. Assume that the query contains ". ", the script is as follows: # grep '\. 'Data. F # grep '\ "'data. F to query the file name conftroll. conf (this is a configuration file). The script is as follows: # grep 'conftroll \. conf 'myfile can contain a maximum of six lower-case characters, followed by a sentence, followed by two upper-case characters # grep '[A-Z \}\. [A-Z] \ {\} 'filename. use of the wildcard * In depositgrep # grep L. * s 'testfile # matching example: "looks" # grep 'oo * 'testfile # matching example: "looks" query all rows whose end of all files contains the word device # grep' Device $ '* query the directories in the directory list, the method is as follows: # ls-L | grep '^ d: # ls-L | grep '^ [^ d: # ls-L | grep '^ d .... X .. x'

Use of regular matching in grep

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.