grep练习(鸟哥私房菜第三版)

以下是练习的文本文件,regular_express.txt

-----------------------------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!^IMy 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!$
go! go! Let's go.$
# I am VBird$

$

--------------------------------------------------------------------------------------------------

例题一:搜寻特定字符串

1 搜寻'the' 

#gerp -n 'the' regular_express.txt

2 反向搜寻,即不包含'the'

#grep -ni 'the' regular_express.txt

例题二:利用中括号[]来搜寻集合字符

1 搜寻test或tast,这两个单词有共同之处:‘t?tst’

#grep -n 't[ae]te' regular_express.txt

其实,[]里面不论有几个字符,都只代表[一个]字符

2寻找有oo的字符

#grep -n 'oo' regular_express.txt

3 不想让oo前面有g

#grep -n '[^g]00' regular_express.txt

4 oo前面不想有小写字母

#grep -n '[^a-z]oo' regular_express.txt

#grep -n '[^[:lower:]oo]' regular_express.txt

 例题三:行首与行尾字符^$

1 列出the出现在行首的行

#grep -n '^the' regular_express.txt

2 要求只出现行首是小写的

#grep -n '^[a-z]' regular_express.txt

#grep -n '^[[:lower:]]' regular_express.txt

(注意:第一种写法成立的条件是设立的语言编码中,小写字母是连续的即a b c d,当为 a A b B ....这样的LANG=zh_TW时,第一种写法就是错误的,所以从这方面讲,我们提倡第二种写法,更多的字母表示例如[:lower:]这种,可以man grep)

3 不想让开头是英文

#grep -n '^[^a-zA-Z]' regular_express.txt

或者

 #grep -n '^[^[:alpha:]]' regular_express.txt

显然^在中括号内外有不同的意义:中括号内为非,即反向选择,中括号外为匹配行首。

4 行尾为小数点

#grep -n '\.$' regular_express.txt

$为匹配行尾,而小数点,需要转义。

5 找出空行

#grep -n '^$' regular_express.txt

例题四: 任意一个字符.与重复字符*

在通配符中*表示任意个字符,但是在正规表达式中含义不同:

“.”(小数点):代表一定有一个任意字符的意思

“*”(星号):   代表重复前一个字符0到任意多次的意思

1 找出g??d的字符串,即共有四个字符,起头是g而结束是d

#grep -n 'g..d' regular_express.txt

2 找出gd,god,good等:

#grep -n 'go*d' regular_express.txt

注意*表示前面字符出现0到任意多次,因此要至少出现两个连续的o,则需要ooo*

3 找出含有数字的行

#grep -n '[0-9][0-9]*' regular_express.txt

例题五:限定连续RE 字符范围{}

1 我想找出连续两个o的字符串,这时需要使用到限定范围字符{},但因为{、}在shell中是有特殊意义的,因此需要用转义字符

# grep -n 'o\{2\}' regular_express.txt

2 我想找出两个o的字符串

#grep -n 'o\{2,5\}' regular_express.txt

3 我想找出连续两个以上o的字符串

#grep -n 'o\{2,\}' regular_express.txt







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值