012_egrep
1. Basic grammar
egrep = grep -E
egrep [OPTIONS] PATTERN [FILE...]
2. Meta-charecter of extended regexp
2.1 Character match
.: Matchs any single character
[]: Matchs any single character within specified range
[^]:Matchs any signal character without specified range
2.2 Matched number
*:Matchs The preceding item zero or more times, refers to only times.
.* : Matchs any item -----Any character of any length
? : Matchs the preceding item zero or at most one times.
+ : Matchs the preceding item at lest one times.
{m} : The preceding item is matched exactly m times
{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
2.3 Anchoring
^ : Only matchs the PATTERN at the begining of a line.
$ : Only matchs the PATTERN at the end of a line.
^PATTERN$ : Matchs the entire line.
^$ : Matchs blank lines
^[[:space:]]*$ : Matchs blank lines
\< or \b:Matchs at the beginning of a word
\> or \b:Matchs at the end of a word
\<PATTERN\>:Matchs the entire word
2.4 group
() : (xy)*ab -- Matchs 'xy' zero or more times.
2.5 reference : The same grammar as grep.
3. Exercise :
1) Print the default shell and UID of the root/centos/user1 user
# grep -E '^(root|centos|user1)\>' /etc/passwd | cut -d: -f1,3,7
2) Print the lines which have a word following with parentheses.
# grep -E -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
3) echo an absolute path and print its base name with egrep;
# echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
further:print the directory of the path,just like the result of command dirname
4) find values between 1-255 of the result of command ifconfig 找出ifconfig命令结果中1-255之间的数值;
5) 找出ifconfig命令结果中的IP地址;
posted on 2018-09-06 20:48 langjitianyadaolao 阅读(100) 评论(0) 编辑 收藏 举报