Linux shell grep command

Source: Internet
Author: User

The general format of Linux shell grep command grep is: www.2cto.com grep [Option] basic regular expression [file]. Here the basic regular expression can be a string. When you enter string parameters in the g r e p command, it is best to enclose the double quotation marks with single quotation marks. Single quotation marks should be used for invocation mode matching. For example, "m y s t r I n g ". There are two reasons for doing this: one is to avoid misunderstanding as the s h e l command, and the other is to find strings composed of multiple words. When you call a variable, double quotation marks should also be used, such as: g r e p "$ m y va r" file name. If not, no results will be returned. Commonly used g r e p options include: www.2cto.com-c only outputs the Count of matched rows. -I is case insensitive (only applicable to single characters ). -H: When querying multiple files, the file name is not displayed. -L only names containing matching characters are output when multiple files are queried. -N: the matching row and row number are displayed. -S does not display the error message that does not exist or does not match the text. -V: displays all rows that do not contain matched text. Example: 1. query multiple files query the word "sort it" $ grep "sort it" * 2. Row Matching 1) in all files) display text containing the "4 8" string: $ grep "48" data. f2) Total number of matched rows output $ grep-c "48" data. f43) number of rows: $ grep-n "48" data. the number of rows f is in the first output column, followed by each matching row containing 4 8. 4) show unmatched rows $ grep-v "48" data. f5) The exact match may have been noticed. In the previous example, the string "4 8" is extracted ", the returned results include other strings such as 4 8 4 and 4 8 3 that contain "4 8". In fact, the rows containing only 4 8 should be precisely extracted. A more effective method for extracting exact matching using g r e p is to add \> after extracting strings. For example, if you want to precisely extract 4 8 data, the method is as follows: $ grep "48 \>" data. f6) by default, g r e p is case sensitive. to query case-insensitive strings, you must use the-I switch. In d a t. the f file contains the month character S e p t, both uppercase and lowercase. To obtain this string, use the following method: $ grep-I "48" data. f 1. Select the location of the city where the code is 4 8 4 and 4 8 3. You can use [] to specify the string range. $ Grep "48 [34]" data. f2. If the row does not match the beginning of the row, make it not 4 or 8. You can use the ^ mark in square brackets. $ Grep "^ [^ 48]" data. f if the string is 48 $ grep-v "^ [^ 48]" data. use the-I switch to disable the case sensitivity of month S e p t. $ grep-I "sept" data. f can also use the [] mode to extract all information of each row containing S e p t and s e p t. $ Grep '[sS] ept 'data. f if you want to extract all the months that contain S e p t, regardless of the case, and this row contains a string of 483, you can use the pipeline command, that is, the output of the command on the left of the symbol "|" is used as the input of the command on the right of "|. Example: [sam @ chenwy sam] $ grep '[sS] ept 'data. f | grep 48483 Sept 5AP1996 USP 65.00 LVX2C 1894. If any character is matched, all codes starting with K and ending with D can be extracted, because the code is known to contain 5 Characters: [sam @ chenwy sam] $ grep 'K... D 'data. f47 Oct 3ZL1998 LPSX 43.00 KVM9D 512483 may 5PA1998 USP 37.00 KVM9D 644 slightly changed the above Code. The first two are uppercase letters, the middle two are arbitrary, and end with C: [sam @ chenwy sam] $ grep '[A-Z] .. C 'data. f5: A common query mode for date query is date query. Query all records whose names start with 5 and end with 1 9 9 6 or 1 9 9 8. Usage mode 5 .. 1 9 [6, 8]. This means that the first character is 5, followed by two vertices, followed by 1 9 9, and the remaining two digits are 6 or 8. [Sam @ chenwy sam] $ grep '5 .. 199 [6, 8] 'data. f6, range combination must learn to use [] to extract information. Assume that you want to obtain the city code. The first character is 0-9, the second character is between 0 and 5, and the third character is between 0 and 6. Use the following mode. [Sam @ chenwy sam] $ grep '[0-9] [0-5 [0-6] 'data. f7: returns the probability of occurrence of all rows that contain numbers 4 at least twice. The method is as follows [sam @ chenwy sam] $ grep '4 \ {2, \} 'data. f8, use grep to match the "and" or "Mode g r e p command plus the-E parameter, this extension allows the use of extended mode matching. For example, the city code to be extracted is 2 1 9 or 2 1 6. The method is as follows: [sam @ chenwy sam] $ grep-E '2017 | 100' data. f9 and empty rows can be used in combination with ^ and $ to query empty rows. Use the-c parameter to display the total number of rows: [sam @ chenwy sam] $ grep-c '^ $' myfile uses the-n parameter to display the actual row: [sam @ chenwy sam] $ grep-n' ^ $'myfile10. query characters with special meanings, such as $. '"* [] ^ | \ +?, Must be preceded by a specific character \. Assume that the query contains ". the script for all rows is as follows: [sam @ chenwy sam] $ grep '\. 'myfile or a double quotation mark: [sam @ chenwy sam] $ grep '\ "'myfile is in the same way, for example, to query the file name c o n f t r o l. c o n f (this is a configuration file). The script is as follows: [sam @ chenwy sam] $ grep 'conftroll \. conf 'myfile11. You can use regular expressions to query formatted file names to match any file name. The system provides standard naming formats for text files. Generally, a maximum of six lower-case characters, followed by a period, followed by two upper-case characters. [Sam @ chenwy sam] $ grep '^ [a-z] \ {1, 6 \}\. [A-Z] \ {1, 2 \} 'filename2, use wildcard * match mode grep "l. * s "testfile: to query a word at the end of a row, try the following mode: grep" ng $ "testfile. This will query all rows whose end contains the word ng in all files.

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.