8、grep和正则表达式

一、egrep

egrep [command line options] [path]

打印指定的数据,填写就是要寻找的字符串

dingyi@martin:~$ egrep 'mellon' sample 
Mark watermellons 12
Oliver rockmellons 2

  • 加==-n==打印行号
  • 加==-c==打印匹配到的行的数量
dingyi@martin:~$ egrep -n  'mellon' sample 
3:Mark watermellons 12
11:Oliver rockmellons 2

dingyi@martin:~$ grep -c 'mellon' sample 
2

二、正则表达式

  • . (dot) - 一个任意字符
  • ? - 前一次字符出现0次或1次
  • *-前一个字符出现次数大于等于0次.
  • + - 前一个字符出现次数大于等于1次
  • {n} - 前一次字符出现n次
  • {n,m} - the preceding character matches at least n times and not more than m times.
  • [agd] - 任意一个括号内的字符
  • [^agd] - 不包含括号里的任一个字符
  • [c-f] - 加个-表示一个范围,这里是c,d,e,f。本质和前面没区别
  • () - allows us to group several characters to behave as one.
  • | (pipe symbol) - the logical OR operation.
  • ^ - 从前往后匹配
  • $ - 从后往前匹配
egrep '[aeiou]{2,}' mysampledata.txt
Robert pears 4
Lisa peaches 7
Anne mangoes 7
Greg pineapples 3


egrep '2.+' mysampledata.txt
Fred apples 20

egrep '2$' mysampledata.txt
Mark watermellons 12
Susy oranges 12
Oliver rockmellons 2

egrep 'or|is|go' mysampledata.txt
Susy oranges 5
Terry oranges 9
Lisa peaches 7
Susy oranges 12
Anne mangoes 7

egrep '^[A-K]' mysampledata.txt
Fred apples 20
Anne mangoes 7
Greg pineapples 3
Betty limes 14
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值