1. grep : Global search Regular expression and Print out the line.
1.1. function:searches the given FILEs for lines containing a match to the given PATTERN.
1.2. Basic grammar : grep [OPTIONS] PATTERN [FILE…]
[root@promote ~]# grep root /etc/passwd
//Print the lines matched 'root'
[root@promote ~]# grep --color=auto root /etc/passwd
//--color=auto highlight the matched strings
[root@promote ~]# fgrep root /etc/passwd
// 快速匹配
1.3 Commondly used options
--color=auto: colours matched items;
-v: Invert the sense of matching, to select non-matching lines;
-i: Ignore case distinctions in both the PATTERN and the input files
-o: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-q: Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
-A #:after, Print NUM lines of trailing context after matching lines. Places a line containing a group separator
-B #: before, Print NUM lines of leading context before matching lines. Places a line containing a group separator
-C #:context, Print NUM lines of output context. Places a line containing a group separator
-E:Interpret PATTERN as an extended regular expression
1.4 Meta-charecter of Regular expression
1.4.1 Character match
.: Matchs any single character
[]: Matchs any single character within specified range
[^]:Matchs any signal character without specified range
[:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]
[root@promote ~]# grep 's..n' /etc/passwd
//查询 /etc/passwd中所有以 s开头以n结尾中间有两个任意字符的字符串
[root@promote ~]# grep '...t' /etc/passwd
//查询 /etc/passwd中所有以 任意三个字符开始以t结尾的字符串
1.4.2 The number of match : Used after the item which need to specify the number of match times,Used to specify the number of match times of The preceding item.
*:Matchs The preceding item zero or more times, refers to only times.
.* : Matchs any item -----Any character of any length
[root@bogon ~]# grep 'r.*t' /etc/passwd
? : Matchs the preceding item zero or at most one times.
[root@bogon ~]# grep 'ro?t' /etc/passwd
+ : Matchs the preceding item at lest one times.
[root@bogon ~]# grep 'ro+t' /etc/passwd
{m} : The preceding item is matched exactly m times
[root@bogon ~]# grep 'ro{2}t' /etc/passwd
{m,n} : The preceding item is matched at lest m times and at most n times.
{0,n} : he preceding item is matched at most n times
{m,} : he preceding item is matched at lest m times
1.4.3 Anchoring
^ : Only matchs the PATTERN at the begining of a line.
[root@bogon ~]# grep '^ro{2}t' /etc/passwd
$ : Only matchs the PATTERN at the end of a line.
[root@bogon ~]# grep 'ro{2}t$' /etc/passwd
^PATTERN$ : Matchs the entire line.
^$ : Matchs blank lines
^[[:space:]]*$ : Matchs blank lines
< or :Matchs at the beginning of a word
> or :Matchs at the end of a word
<PATTERN>:Matchs the entire word
[root@bogon ~]# grep '<ro{2}t' /etc/passwd
1.3.4 group : process multiple characters as a whole
() : (xy)*ab -- Matchs 'xy' zero or more times.
1.3.5 reference
[root@bogon ~]# grep '(<ro{2}t).*1' /etc/passwd
'(<ro{2}t).*1' is different from '(<ro{2}t).*(<ro{2}t)'
If you don't get it,you need to have a try.
2. exercise
1) Print the lines beginning with 's' or 'S' in /proc/meminfo file with two methods.
2) Print the lines of /etc/passwd file which are not end with /bin/bash.
3) Print the user name saved in the /etc/passwd file which the name has the biggest ID value.
4) Print the default shell if the root user exists;
# id root &> /dev/null && grep "^root>" /etc/passwd | cut -d: -f7
5) Find two or three digit value in /etc/passwd;
# grep "<[0-9]{2,3}>" /etc/passwd
6) Print the lines beginning with a space and ending with non-space in /etc/rc.d/rc.sysinit file;
# grep "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg
7) Print the lines ending with zero or more spaces of the result of executing "netstat -tan"
# netstat -tan | grep "LISTEN[[:space:]]*$"
8) Add users named bash、testbash、basher and nologin(and the shell is /sbin/nologin); and then find the lines which have the same name as its shell name.
# grep "^([[:alnum:]]+>).*1$" /etc/passwd