shell编程-正则表达式
2016-06-29 13:30
459 查看
1、正则表达式是什么
正则表达式是用于字符排列和匹配模式的一种语法规则。它主要用于字符串的模式分割、匹配、查找及替换操作。2、正则表达式与通配符
正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、awk、sed等命令可以支持正则表达式。通配符用来匹配符合条件的文件名,通配符是完全匹配。ls、find、cp这些命令不支持正则表达式,所以只能使用shell自己的通配符进行匹配了。
通配符:
*:匹配任意内容
?: 匹配任意一个内容
[]:匹配中括号中的一个字符
3、基础正则表达式
元字符 | 作用 |
---|---|
* | 前一个字符匹配0次或任意多次。 |
. | 匹配除了换行符外任意一个字符。 |
^ | 匹配行首。例如^hello会匹配以hello开头的行。 |
$ | 匹配行尾。例如hello$会匹配以hello结尾的行。 |
[] | 匹配中括号中指定的任意一个字符,只匹配一个字符。例如[aoeiu]匹配任意一个元音字符,[0-9]匹配任意一个数字,[a-z][0-9]匹配小写字和一位数字构成的两位字符 |
[^] | 匹配除中括号的字符以外的任意一个字符。例如:[^0-9]匹配任意一位非数字字符,[^a-z]表示任意一位非小写字母。 |
\ | 转义符。用于取消讲特殊符号的含义取消。 |
\{n,\} | 表示其前面的字符恰好出现n次。例如[0-9]\{4\}匹配4位数字, |
\{n,m\} | 表示前面的字符至少出现一次,最多出现m次。例如[a-z]\{6,8}匹配6到8位的小写字母。 |
?()属于扩展正则
修改~/.bashrc配置文件,为grep起别名,将匹配出来的用颜色显示alias grep=’grep –color=auto’
例如:*号的示例
[root@localhost home]# grep aa* test.txt a aa aaa aaaa aaaaa ab aabb
例如
“.”:匹配除了换行符外任意一个字符
“s..d”:会匹配在s和d这两个字母之间一定有两个字符的单词
[root@localhost home]# grep "s..d" test.txt said soid sdkdkdkd
“s.*d”:匹配在s和d字母之间有任意字符
[root@localhost home]# grep "s.*d" test.txt said soid sdkdkdkd
“.*”:匹配所有内容
[root@localhost home]# grep ".*" test.txt a aa aaa aaaa aaaaa ab aabb b bb bbb bbbb bbbb said soid sdkdkdkd
例如
“^”匹配行首,”$”匹配行尾
“^M”:匹配以大写“M”开头的行
[root@localhost home]# grep "^a" test.txt a aa aaa aaaa aaaaa ab aabb
“n$”:匹配以小写“n”结尾的行
[root@localhost home]# grep "b$" test.txt ab aabb b bb bbb bbbb bbbb
“^$”:匹配空白行显示,增加-n选项,显示行号
[root@localhost home]# grep -n "^$" test.txt 6: 9: 15: 16:
例如
“[]” : 匹配中括号中指定的任意一个字符,只匹配一个字符。
“s[ao]id”:匹配s和i字母中间,要么是a,要么是o的字符串
[root@localhost home]# grep -n "s[ao]id" test.txt 17:said 18:soid
“[0-9]”:匹配包括任意一个数字的行
[root@localhost home]# grep -n "[0-9]" test.txt 22:6adfadfadf8 23:jladf8002378 25:ladfjadf7 27:3333333333 29:2222222222
“^[a-z]”:匹配用小写字母开头的行
[root@localhost home]# grep "^[a-z]" test.txt a aa aaa aaaa aaaaa ab aabb b bb bbb bbbb bbbb said soid sdkdkdkd jladf8002378 ladfjadf7
“[a-z]$”:匹配用小写字母结尾的行
[root@localhost home]# grep "[a-z]$" test.txt a aa aaa aaaa aaaaa ab aabb b bb bbb bbbb bbbb said soid sdkdkdkd [root@localhost home]# grep "^[a-z]$" test.txt a b [root@localhost home]# grep "^[a-z][a-z]$" test.txt aa ab bb
“[^]”:匹配除中括号的字符以外的任意字符。(说明:^放在中括号内)
“^[^a-z]”:匹配不用小写字母开头的行
[root@localhost home]# grep "^[^a-z]" test.txt 6adfadfadf8 3333333333 2222222222
“^[^a-zA-Z]”:匹配不用字母开头的行
[root@localhost home]# grep "^[^a-zA-Z]" test.txt 6adfadfadf8 3333333333 2222222222
“\”:转义字符,作用让特殊的字符丧失意义
“.$”:匹配使用”.”结尾的行
[root@localhost home]# grep "\.$" test.txt 6adfadfadf8.
“\{n\}”:表示其前面的字符恰好出现n次
“a\{3\}”:匹配a字母连续出现三次的字符串。
注意:至少重复3次,匹配出来的结果显示超过3个字符串也会显示到结果中
[root@localhost home]# grep "a\{3\}" test.txt aaa aaaa aaaaa
“[0-9]\{3\}”:匹配包括连续的三个数字的字符串
要想准确查找,需要加定界符
相关文章推荐
- SHELL相关
- shell 初识
- PowerShell 使用当前日期创建文件夹
- Linux学习之XShell与虚拟机的连接
- linux shell 实用脚本
- shell小脚本
- (OK)(OK) adb -s emulator-5554 shell - natpf1 - VirtualBox - NAT Port Forwarding
- shell 编程基础
- shell中的条件判断
- shell 获取系统时间
- 使用shell和python分别实现简单菜单功能--打印当前系统状态信息
- XShell 快捷键
- SHELL--待续
- shell中的if-then语句
- SHELL实现CentOS6,Ubuntu16下自动下载,编译安装LAMP
- Linux:-bash: ***: command not found
- Android之adb shell dumpsys activity获取task里面的所有actitiy
- sed的工作原理及使用
- Shell 循环语句:if,case esac,for,while
- 如何在C语言中调用shell命令