53. 正则表达式

阅读: 评论:0

53. 正则表达式

53. 正则表达式

  • grep:在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行
  • egrep:扩展的egrep,支持更多的正则表达式元字符
  • fgrep:固定grep(fixed grep),,有时候也被称之为快速fast grep,它按字面解释所有的字符
  • grep -q选项:不输出结果
  • grep [选项] PATTERN filename
grep 'Tom' /etc/passwd
找到:grep的退出状态为0
找不到:grep的退出状态为1
找不到指定文件:grep的退出状态为2

 grep程序的输入可以来自标准输入或者管道,而不仅仅是文件

ps aux|grep 'sshd'

grep:使用基本元字符集^,$,.,*,[],[ ^ ], < >, ( ), { }, +, |
*表示前导符,[]表示括号里面任意一个, < >表示词首词尾, ( )表示标签或者字符组, { }表示匹配n次

egrep(或者grep -E):使用扩展元字符集?,+,{},|,()
(1)注意:grep也可以使用扩展集中的元字符,仅需要对这些元字符前置一个反斜线
(2)w 所有字母与数字,称之为字符[a-zA-Z0-9],‘l[a-zA-Z0-9]ove’,‘lwove’
(3)W 所有字母与数字之外的字符,称之为非字符,‘love[^a-zA-Z0-9]+’,‘loveW’
 

greo -E 或者egrep
egrep 'NW' datafile		找有NW的行
egrep 'NW' d*		找以d开始的所有文件
egrep '^n' datafile		找以n开始的行
egrep '4$' datafile			找以4结尾的行
egrep TB Savage datafile		找TB
egrep 'TB Savage' datafile	找TB Savage
egrep '5..' datafile		找5.任意一个字符
egrep '.5' datafile		找.5的行
egrep '^[we]' datafile		找we开头的行
egrep '[^0-9]' datafile		找非数字行
egrep '[A-Z][A-Z] [A-Z]' datafile		找大写字母大写字母空格大写字母的行
egrep 'ss*' datafile		找至少一个s		egrep 's+' datafile	
egrep '[a-z]{9}' datafile		找连续出现9个相同英文字母
egrep '<north' datafile		找north词首
egrep '<north>' datafile		找north单词
egrep '<[a-r].*n>' datafile		找a-r开头,n结尾的单词
egrep '^nw*W' datafile		以n开头的字符,跟着非字符
egrep 'bnorthb' datafile		找north单词egrep 'NW|EA' datafile		找NW或EA的行
egrep '3+' datafile		找1到多个3的行
egrep '2.?[0-9]' datafile		找2零到1个点,接着数字
egrep '(no)+' datafile		找no出现1到n次
egrep 'S(h|u)' datafile		Sh或Su
egrep 'Sh|u' datafile		Sh或u

 

grep --help|grep '-v'		##-v这里是选项
grep -R '=~' /root/		目录
grep '=~' /root/*		文件
颜色的话,在vim .bashrc中添加:
alias grep='grep --color'grep -B2 'root' /etc/passwd			还包括root的行的前两行
grep -A2 'root' /etc/passwd			还包括root的行的后两行
grep -C2 'root' /etc/passwd			还包括root的行的前两行和后两行

[^#] :以#开头

^[^#]不以#开头

用转义符-v 表示选项-v

 如果过滤的内部不知道在哪个目录下,则用递归

[root@localhost ~]# grep -R '=~' /root/*
/root/code/read_num.sh: if [[ "$num" =~ ^[0-9]+$ ]];then
/root/code/user_add.sh:if [[ ! "$num" =~ ^[0-9]+$ || "$num" =~ ^0+$ ]];then
Binary file /root/paramiko/.git/objects/pack/pack-e86773a3e9f953686832306b8d266bfda93018bb.pack matches
Binary file /root/paramiko/.git/objects/pack/pack-e86773a3e9f953686832306b8d266bfda93018bb.idx matches
/root/paramiko/tests/fig:    IdentityFile    =~/.ssh/id_rsa
Binary file /root/paramiko/dist/paramiko-2.6. matchesroot@localhost ~]# grep -r '=~' /root/*
/root/code/read_num.sh: if [[ "$num" =~ ^[0-9]+$ ]];then
/root/code/user_add.sh:if [[ ! "$num" =~ ^[0-9]+$ || "$num" =~ ^0+$ ]];then
Binary file /root/paramiko/.git/objects/pack/pack-e86773a3e9f953686832306b8d266bfda93018bb.pack matches
Binary file /root/paramiko/.git/objects/pack/pack-e86773a3e9f953686832306b8d266bfda93018bb.idx matches
/root/paramiko/tests/fig:    IdentityFile    =~/.ssh/id_rsa
Binary file /root/paramiko/dist/paramiko-2.6. matches[root@localhost ~]# grep '=~' /root/*
grep: /root/c: Is a directory
grep: /root/code: Is a directory
grep: /root/paramiko: Is a directory
grep: /root/test: Is a directory

 

本文发布于:2024-02-01 21:30:54,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170679425439539.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:正则表达式
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23