Linux Shell Basics (ii)

Source: Internet
Author: User
Tags egrep

Seven. grep family:

1. grep exit Status:
0: Indicates success;
1: Indicates that a matching pattern could not be found in the supplied file;
2: Indicates that the file provided in the parameter does not exist.
See the example below:
/> grep ' root '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root :/sbin/nologin
/> echo $?
0

/> grep ' root1 '/etc/passwd #用户root1并不存在
/> echo $?
1
/> grep ' root '/etc/passwd1 #这里的/etc/passwd1 file does not exist
grep:/etc/passwd1:no such file or directory
/> Echo $?
2

2. Use an instance of a regular expression in grep:
It is necessary to note that the following regular expressions are described in detail in the previous article, so in the following example, you can see the regular description section of the previous article.
/> Cat testfile
Northwest NW Charles Main 3.0.98 3
Western WE Sharon Gray 5.3.5 +
Southwest S W Lewis dalsass 2.7.8 2
Southern so Suan Chin 5.1.4
Southeast SE Patricia Hemenway 4.0.7 4
East Ern EA TB Savage 4.4 5
Northeast NE AM Main Jr 5.1.94 3
North No. Margot Weber 4.5.5 9
Central CT ANN Stephens 5.7 94 5


/> grep NW testfile #打印出testfile中所有包含NW的行.
Northwest NW Charles Main 3.0 98 3 +

/> grep ' ^n ' testfile #打印出以n开头的行.
Northwest NW Charles Main 3.0 98 3 Northeast NE AM Main Jr 5.1.94 3
North NO Margot Weber 4.5.89 5 9

/> grep ' 4$ ' testfile #打印出以4结尾的行.
Northwest NW Charles Main 3.0 98 3 x

/> grep ' 5\: ' Testfile #打印出第一个字符是5, followed by a. Character followed by a line of any character.
Western WE Sharon Gray 5.3.5
Southern So Suan Chin 5.1.4
Northeast NE AM Main Jr. 5.1.94 3 13< br> Central CT Ann Stephens 5.7 94 5

/> grep ' \.5 ' testfile #打印出所有包含. 5 line.
North NO Margot Weber 4.5.5 9

/> grep ' ^[we] ' testfile #打印出所有以w或e开头的行.
Western WE Sharon Gray 5.3.97 5 23
Eastern EA TB Savage 4.4.84 5 20

/> grep ' [^0-9] ' testfile #打印出所有不是以0-9 line begins.
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 Savage 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 Stephens 5.7.94 5 13

/> grep ' [a-z][a-z] [A-z] ' testfile #打印出所有包含前两个字符是大写字符, followed by a space and a line of uppercase letters.
Eastern EA TB Savage 4.4.84 5 20
Northeast NE AM Main Jr. 5.1.94 3 13
Note: If you do not get the expected result when you execute the above command, that is, grep ignores case, the cause of this problem is likely to be a localized setup issue for the current environment. For the above command, if I set the current language to en_US, it will print out all the rows, when I change it to the Chinese environment, I can get my current output.
/> Export LANG=ZH_CN #设置当前的语言环境为中文.
/> Export Lang=en_us #设置当前的语言环境为美国.
/> Export Lang=en_br #设置当前的语言环境为英国.

/> grep ' [a-z]\{9\} ' Testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行.
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

#第一个字符是3, followed by a period, then any number, then any arbitrary character, then another 3, then a tab, then another 3, it should be noted that the following regular \1 represents \ (3\).
/> grep ' \ (3\) \. [0-9].*\1 *\1 ' testfile
Northwest NW Charles Main 3.0.98 3 34

/> grep ' \<north ' testfile #打印所有以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 #打印所有包含单词north的行.
North NO Margot Weber 4.5.89 5 9

/> grep ' ^n\w* ' testfile #第一个字符是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. Expand grep (GREP-E or Egrep):
The main benefit of using the extended grep is the addition of additional regular expression meta-character sets. Let's continue with the example to demonstrate the extension grep.
/> Egrep ' nw| EA ' Testfile #打印所有包含NW或EA的行. If you are not using Egrep, but grep, there will be no results detected.
Northwest NW Charles Main 3.0.98 3 34
Eastern EA TB Savage 4.4.84 5 20

/> grep ' nw\| EA ' Testfile #对于标准grep, if the extension metacharacters are preceded by \,grep, the extended option is automatically enabled-E.
Northwest NW Charles Main 3.0.98 3 34
Eastern EA TB Savage 4.4.84 5 20

/> Egrep ' testfile '
/> grep-e ' testfile '
/> grep ' 3\+ ' testfile #这3条命令将会打印出相同的结果, i.e. all rows containing one or more 3.
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 Stephens 5.7.94 5 13

/> Egrep ' 2\.? [0-9] ' testfile
/> grep-e ' 2\.? [0-9] ' testfile
/> grep ' 2\.\? [0-9] ' testfile #首先含有2字符, followed by 0 or 1 points, 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 Savage 4.4.84 5 20

/> Egrep ' (no) + ' testfile
/> Grep-e ' (no) + ' testfile
/> grep ' \ (no\) \+ ' Testfile #3个命令返回相同结果, which prints one or more contiguous 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 #首先是一个或者多个字母, followed by one or more non-alphanumeric numbers, 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 Stephens 5.7.94 5 13

/> Egrep ' [Ss] (h|u) ' Testfile
/> Grep-e ' [Ss] (h|u) ' Testfile
/> grep ' [ss]\ (h\|u\) ' Testfile #3个命令返回相同结果, which begins with S or S, followed by a line of 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 #west开头, where ES is the value of \1, followed by any number of arbitrary characters, and finally an ES appears on the line.
Northwest NW Charles Main 3.0.98 3 34

4. grep options:
Here is a list of the command-line options commonly used by grep:

Option description
-C shows only how many rows match, not exactly the matching rows.
-H does not display the file name.
-I ignores case when comparing strings.
-L Displays a list of file names only for rows that contain matching templates.
-L Displays a list of file names only for rows that do not contain matching templates.
-N Prints the number of rows in the file before each line.
-V reverse retrieval, showing only rows that do not match.
-W displays only the matches of the complete word.
-X displays only the matching of the complete row.
-r/-r If the file parameter is a directory, this option recursively searches for all subdirectories and files under that directory.
/> grep-n ' ^south ' testfile #-n option prints the line number in front of each matching line.
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 off case sensitive.
Southeast SE Patricia Hemenway 4.0.7 4 17

/> grep-v ' Suan Chin ' testfile #打印所有不包含Suan Chin line.
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 Savage 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 Stephens 5.7.94 5 13

/> grep-l ' ss ' testfile #-l makes grep print only matching file names, not matching rows.
Testfile

/> grep-c ' West ' testfile #-c makes grep print only the number of rows that match the template.
3

/> grep-w ' North ' testfile #-w only prints lines that match the entire word.
North NO Margot Weber 4.5.89 5 9

/> grep-c 2 Patricia testfile #打印匹配行及其上下各两行.
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 Savage 4.4.84 5 20
Northeast NE AM Main Jr. 5.1.94 3 13

/> Grep-b 2 Patricia testfile #打印匹配行及其前两行.
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 #打印匹配行及其后两行.
Southeast SE Patricia Hemenway 4.0.7 4 17
Eastern EA TB Savage 4.4.84 5 20
Northeast NE AM Main Jr. 5.1.94 3 13

Linux Shell Basics (ii)

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.