基础正则表达式练习(鸟哥)

命令:
grep :分析一行信息,若当做有我们所需要的信息就拿出来。
-a:将binary文件text文件的方式查找数据
-c:计算找到‘查找字符串’的次数
-i:忽略大小写
-n:顺便输出行号
-v:反向选择(把它看做去除也可以)
--color=auto:可以将找到的关键字的部分加上颜色
 
语系设置为LANG=C

设置别名grep显示颜色


[root@bogon ~]# vim /etc/sysconfig/i18n 
LANG="C"
SYSFONT="latarcyrheb-sun16"
[root@bogon ~]# vim /etc/profile.d/grep.sh
alias grep="grep --color=auto"
[root@bogon ~]# source /etc/profile.d/grep.sh 
[root@bogon ~]# source /etc/sysconfig/i18n 
[root@bogon ~]# echo $LANG
C


[root@bogon ~]# alias
alias cp='cp -i'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
下载鸟哥范例文件
wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
文件内容
[root@bogon ~]# vim regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
一.查找特定字符串
grep -n 'the' regular_express.txt
8:I can't finish the test.
12: the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
反向选择
grep -vn 'the' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh!    My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:
注:grep -v代表反向查找,上面就是查找没有'the'这个字符串的行
忽略大小写
grep -in 'the' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12: the symbol '*' is represented as start.
14: The gd software is a library for drafting programs.
15:You are the best is mean you are the no. 1.
16: The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
二.利用中括号[]来查找集合字符
grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
注:中括号中的字符一次只能匹配其中一个
grep -n 'oo' regular_express.txt
1:"Open Source" is a g ood mechanism to develop programs.
2:apple is my favorite f ood.
3:Football game is not use feet only.
9:Oh! The soup taste g ood.
18:g oogle is the best tools for search keyword.
19:g oooooogle yes!
如果不想匹配类型oo前面有g的话
grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3: Football game is not use feet only.
18:google is the best tools for search keyword.
19:g oooooogle yes!
oo前面不要小写字符
grep -n '[^a-z]oo' regular_express.txt
3: Football game is not use feet only.
想要有数字的那一行
grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
前面两个匹配的也使用[:lower:].[:digit:]等字符,也可以实现相同的功能。
三.行尾与行首字符^$
让the在行首出现
grep -n '^the' regular_express.txt
12: the symbol '*' is represented as start.
开头是小写字符的行
grep -n '^[a-z]' regular_express.txt
2: apple is my favorite food.
4: this dress doesn't fit me.
10: motorcycle is cheap than car.
12: the symbol '*' is represented as start.
18: google is the best tools for search keyword.
19: goooooogle yes!
20: go! go! Let's go.
不想开头是英文字母
grep -n '^[^a-zA-Z]' regular_express.txt
1: "Open Source" is a good mechanism to develop programs.
21: # I am VBird
注:^在中括号[]中代表”反向选择“,在括号[]之外则代表定位在行首的意思。
 
要行尾是小数点.的那一行
grep '\.$' regular_express.txt
"Open Source" is a good mechanism to develop programs .
apple is my favorite food .
Football game is not use feet only .
this dress doesn't fit me .
motorcycle is cheap than car .
This window is clear .
the symbol '*' is represented as start .
You are the best is mean you are the no. 1 .
The world <Happy> is the same with "glad" .
I like dog .
google is the best tools for search keyword .
go! go! Let's go .
 
找出一行是空白行
grep -n '^$' regular_express.txt
22:
 
去除配置文件中的空白行跟注释行
grep -v '^$' /etc/syslog.conf | grep -v '^#'
四.任意一个字符.与多重字符*
需要g??d的字符
grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with " glad".
 
需要两个以上的o
grep -n 'ooo*' regular_express.txt
1:"Open Source" is a g ood mechanism to develop programs.
2:apple is my favorite f ood.
3:F ootball game is not use feet only.
9:Oh! The soup taste g ood.
18:g oogle is the best t ools for search keyword.
19:g oooooogle yes!
 
开头结尾都是g,但是两个g之间仅能存在至少一个o
grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
 
要开头与结尾都是g的字符串
grep -n 'g.*g' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18: google is the best tools for search keyword.
19: goooooogle yes!
20: go! go! Let's go.
 
找出任意数字
grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
 
注:*代表的是重复0个或多个前面RE字符的意义,因此,“o*”代表的是具有空字符或一个以上的字符,特别注意,因为允许空字符(就是没有字符都可以的意思)“oo*”则第一个o肯定必须要存在,第二个o则是可有可无的多个o
“.*”代表零个或多个任意字符的意思
 
五.限定连续RE字符范围{}
找到两个o的字符串
grep -n 'o\{2\}' regular_express.txt
1:"Open Source" is a g ood mechanism to develop programs.
2:apple is my favorite f ood.
3:F ootball game is not use feet only.
9:Oh! The soup taste g ood.
18:g oogle is the best t ools for search keyword.
19:g oooooogle yes!
 
找出g后面接2到5个o
grep -n 'go\{2,5\}g' regular_express.txt
18:g oogle is the best tools for search keyword.
 
找2个o以上的goooo....g
grep -n 'go\{2,\}g' regular_express.txt
18:g oogle is the best tools for search keyword.
19:g oooooogle yes!
 
 

转载于:https://my.oschina.net/f91jty/blog/168325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值