一、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