Common CentOS Shell skills grep

Source: Internet
Author: User
Tags egrep

Grep family:

1. grep exit status:
0: indicates successful;
1: The matching pattern cannot be found in the provided file;
2: The file provided in the parameter does not exist.
See the following example:
/> Grep 'root'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Operator: x: 11: 0: operator:/root:/sbin/nologin
/> Echo $?
0

/> Grep 'root1'/etc/passwd# User root1 does not exist
/> Echo $?
1

/> Grep 'root'/etc/passwd1# The/etc/passwd1 file does not exist.
Grep:/etc/passwd1: No such file or directory
/> Echo $?
2

2. Example of applying a regular expression in grep:
It must be noted that the following regular expressions have been described in detail in the previous article. Therefore, when you look at the example below, you can combine them with the regular expression instructions in the previous article.
/> Cat testfile
Northwest NW Charles Main 3.0. 98 3 34
Western WE Sharon Gray 5.3. 97 5 23
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southern SO Suan Chin 5.1. 95 4 15
Southeast SE Patricia Hemenway 4.0. 7 4 17
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9
Central CT Ann Stephen 5.7. 94 5 13


/> Grep NW testfile# Print all rows in testfile that contain NW.
Northwest NW Charles Main 3.0. 98 3 34

/> Grep '^ n' testfile# Print the rows starting with n.
Northwest NW Charles Main 3.0. 98 3 34
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9

/> Grep '4 $ 'testfile# Print the row ending with 4.
Northwest NW Charles Main 3.0. 98 3 34

/> Grep '5 \ .. 'testfile# Print the first character 5, followed by A. character, followed by any character line.
Western WE Sharon Gray 5.3. 97 5 23
Southern SO Suan Chin 5.1. 95 4 15
Northeast ne am Main Jr. 5.1. 94 3 13
Central CT Ann Stephen 5.7. 94 5 13

/> Grep '\. 5' testfile# Print all rows containing. 5.
North NO Margot Weber 4.5. 89 5 9

/> Grep '^ [we] 'testfile# Print all rows starting with w or e.
Western WE Sharon Gray 5.3. 97 5 23
Eastern ea tb save age 4.4. 84 5 20

/> Grep '[^ 0-9] 'testfile# Print out all rows not starting with 0-9.
Northwest NW Charles Main 3.0. 98 3 34
Western WE Sharon Gray 5.3. 97 5 23
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southern SO Suan Chin 5.1. 95 4 15
Southeast SE Patricia Hemenway 4.0. 7 4 17
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9
Central CT Ann Stephen 5.7. 94 5 13

/> Grep '[A-Z] [A-Z] [A-Z] 'testfile# Print all rows that contain the first two uppercase characters followed by a space and an uppercase letter.
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13
Note: When executing the preceding command, if the expected result cannot be obtained, that is, grep ignores case sensitivity, the cause of this problem may be the local setting of the current environment. For the above commands, if I set the current language to en_US, it will print all the lines. When I change it to the Chinese environment, now I can get the output.
/> Export LANG = zh_CN# Set the current language environment to Chinese.
/> Export LANG = en_US# Set the current language environment to the United States.
/> Export LANG = en_Br# Set the current language environment to UK.

/> Grep '[a-z] \ {9 \}' testfile# Print all rows of a string that contains at least nine consecutive lowercase characters.
Northwest NW Charles Main 3.0. 98 3 34
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southeast SE Patricia Hemenway 4.0. 7 4 17
Northeast ne am Main Jr. 5.1. 94 3 13

# The first character is 3, followed by a period, followed by any number, followed by any character, followed by a 3 character, followed by a tab, followed by a 3 character, it must be noted that \ 1 in the following regular expression \ (3 \).
/> Grep '\ (3 \) \. [0-9]. * \ 1 * \ 1' testfile
Northwest NW Charles Main 3.0. 98 3 34

/> Grep '\ <north' testfile# Print all rows starting with north.
Northwest NW Charles Main 3.0. 98 3 34
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9

/> Grep '\ <north \> 'testfile# Print all rows containing the word "north.
North NO Margot Weber 4.5. 89 5 9

/> Grep '^ n \ w *' testfile# The first character is n, followed by any letter or number.
Northwest NW Charles Main 3.0. 98 3 34
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9

3. Extended grep (grep-E or egrep ):
The main benefit of using extended grep is the addition of an additional regular expression metacharacter set. Next we will continue to use the instance to demonstrate the extended grep.
/> Egrep 'nw | EA 'testfile# Print all rows that contain NW or EA. If you do not use egrep but grep, no results will be found.
Northwest NW Charles Main 3.0. 98 3 34
Eastern ea tb save age 4.4. 84 5 20

/> Grep 'nw \ | EA 'testfile# For standard grep, if \ is added before the extended metacharacters, grep automatically enables the extended option-E.
Northwest NW Charles Main 3.0. 98 3 34
Eastern ea tb save age 4.4. 84 5 20

/> Egrep '3 + 'testfile
/> Grep-E '3 + 'testfile
/> Grep '3 \ + 'testfile# The Three commands print the same result, that is, all rows containing one or more three.
Northwest NW Charles Main 3.0. 98 3 34
Western WE Sharon Gray 5.3. 97 5 23
Northeast ne am Main Jr. 5.1. 94 3 13
Central CT Ann Stephen 5.7. 94 5 13

/> Egrep '2 \.? [0-9] 'testfile
/> Grep-E '2 \.? [0-9] 'testfile
/> Grep '2 \.\? [0-9] 'testfile# It must contain 2 characters, followed by 0 or 1 point, followed by a number between 0 and 9.
Western WE Sharon Gray 5.3. 97 5 23
Southwest SW Lewis Dalsass 2.7. 8 2 18
Eastern ea tb save age 4.4. 84 5 20

/> Egrep '(no) + 'testfile
/> Grep-E '(no) + 'testfile
/> Grep '\ (no \) \ + 'testfile# Three commands return the same result, that is, printing one or more consecutive no rows.
Northwest NW Charles Main 3.0. 98 3 34
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9

/> Grep-E '\ w + \ W + [ABC] 'testfile# First, one or more letters followed by one or more non-alphanumeric characters, and the last one in ABC.
Northwest NW Charles Main 3.0. 98 3 34
Southern SO Suan Chin 5.1. 95 4 15
Northeast ne am Main Jr. 5.1. 94 3 13
Central CT Ann Stephen 5.7. 94 5 13

/> Egrep '[Ss] (h | u)' testfile
/> Grep-E '[Ss] (h | u)' testfile
/> Grep '[Ss] \ (h \ | u \) 'testfile# The Three commands return the same result, that is, the line starting with S or s followed by h or u.
Western WE Sharon Gray 5.3. 97 5 23
Southern SO Suan Chin 5.1. 95 4 15

/> Egrep 'W (es) t. * \ 1' testfile# Start with west, where es is the value of \ 1, followed by any number of characters, and finally an es appears in this row.
Northwest NW Charles Main 3.0. 98 3 34

4. grep options:
The following lists common command line options of grep:

Option description-c only shows how many rows match, not the matching rows. -H does not display the file name. -I ignore case sensitivity when comparing strings. -L only displays the list of file names of rows that match the template. -L only displays the list of file names of rows that do not contain matching templates. -N: print the number of rows in the file before each row. -V reverse search: Only unmatched rows are displayed. -W only shows matching of the complete word. -X only shows matching of the complete row. -R/-R if the file parameter is a directory, this option recursively searches all subdirectories and files in the directory.

/> Grep-n' ^ South' testfileThe #-n option prints the row number before each matching row.
3: southwest SW Lewis Dalsass 2.7. 8 2 18
4: southern SO Suan Chin 5.1. 95 4 15
5: southeast SE Patricia Hemenway 4.0. 7 4 17

/> Grep-I 'pat 'testfile#-I option disables case sensitivity.
Southeast SE Patricia Hemenway 4.0. 7 4 17

/> Grep-v 'suan chin' testfile# Print all rows that do not contain Suan Chin.
Northwest NW Charles Main 3.0. 98 3 34
Western WE Sharon Gray 5.3. 97 5 23
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southeast SE Patricia Hemenway 4.0. 7 4 17
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13
North NO Margot Weber 4.5. 89 5 9
Central CT Ann Stephen 5.7. 94 5 13

/> Grep-l 'ss' testfile#-L enables grep to print only the matched file name, without printing the matched rows.
Testfile

/> Grep-c 'west' testfile#-C makes grep only print the number of rows matching the template.
3

/> Grep-w 'North 'testfile#-W only prints the rows matching the entire word.
North NO Margot Weber 4.5. 89 5 9

/> Grep-C 2 Patricia testfile# Print the matching rows and the upper and lower rows.
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southern SO Suan Chin 5.1. 95 4 15
Southeast SE Patricia Hemenway 4.0. 7 4 17
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13

/> Grep-B 2 Patricia testfile# Print the matching row and the first two rows.
Southwest SW Lewis Dalsass 2.7. 8 2 18
Southern SO Suan Chin 5.1. 95 4 15
Southeast SE Patricia Hemenway 4.0. 7 4 17

/> Grep-A 2 Patricia testfile# Print the matching row and the last two rows.
Southeast SE Patricia Hemenway 4.0. 7 4 17
Eastern ea tb save age 4.4. 84 5 20
Northeast ne am Main Jr. 5.1. 94 3 13

Related Article

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.