shell学习之正则表达式基础篇
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell学习之正则表达式基础篇相关的知识,希望对你有一定的参考价值。
什么是正则表达式:
是你定义的、linux工具用来过滤文本的模式模板。Linux工具(比如sed编辑器或gawk)能够在数据流向工具时对数据进行正则表达式模式匹配。如果数据匹配模式,它就会被接受并进一步处理。如果数据不匹配模式,它就会被过滤掉。
|———> 匹配的数据
|
数据流———>正则表达式 ————— | |
|———> 不匹配的数据
正则表达式用来在文件中匹配符合条件的字符串,正则包含匹配。Grep、awk、sed等都可以支持正则表达式。
正则表达式目的:
给指定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”)
可以通过正则表达式,从字符串中获取我们想要的特定部分。
正则表达式特点:
1.灵活性、逻辑性和功能性非常的强
2.可以迅速地用极简单的方式达到字符串的复杂控制
3.对于刚接触的来说,比较晦涩难懂。
由于正则表达式主要应用对象是文本,因此它在各种文本编辑器场合都有应用,系哦啊到著名编辑器EditPlus,大到Microsoft Word、Visual Studio等大型编辑器,都可以使用正则表达式来处理文本内容。
正则表达式与通配符的区别:
1.正则表达式是通过字符串过滤文本内容,通配符则是过滤文件名。
2.正则表达式是包含匹配,通配符是完全匹配。
举例:包含匹配与完全匹配的意思。(个人定义用词)
[[email protected] ~]# touch aa [[email protected] ~]# touch aabb 创建文件aa,aabb用ls查找。 [[email protected] ~]# ls aa 只查找出aa,而aabb并没有在结果中,这种就是完全匹配的意思。 Aa [[email protected] ~]# [[email protected] lamp]# cat abc.txt 查看文件(abc.txt文件内容这里只是演示用的) When I was young I‘d listen to the radio Waiting for my favorite songs When they played I‘d sing along It make me smile Those were such happy times and not so long ago How I wondered where they‘d gone But they‘re back again just like a long lost friend All the songs I love so well Every sha la la la sha la la la every wo wo wo wo still shines Every sing aling aling sing aling aling that [[email protected] lamp]# grep "When" abc.txt 在abc.txt文件中过滤字符串When。 [[email protected] lamp]# grep "W" abc.txt 在abc.txt文件中过滤字符串W内容。显示结果会把所有包含W字符的内容都过滤出来。这就是包含匹配 When I was young I‘d listen to the radio Waiting for my favorite songs When they played I‘d sing along
基础正则表达式字符
字符 | 含义 |
* | 匹配前一个字符出现0次或多次 |
. | 匹配任意单个字符 |
^ | 匹配行首。 |
$ | 匹配行尾。 |
[] | 匹配中括号指定的任意一个字符 |
[^] | 匹配出中括号的字符以外的任意一个字符 |
\ | 匹配转义后的字符串 |
\{n\} | 匹配前一个字符重复n次 |
\{n,\} | 匹配前一个字符至少重复n次 |
\{n,m\} | 匹配前一个字符重复n到m次 |
举例 * 匹配前一个字符出现0次或多次
[[email protected] lamp]# grep "W*" abc.txt 对于初学者一般认为是过滤包含W的行或者已W开头的行。错。W并不能说明就是第一个字符,在正则当中行首需要用^号表示。根据*号含义。匹配W字符的前一个字符出现0次或多次,这个是没有意义的。即使是没有文件的空白行,也会在匹配结果中输出。 When I was young I‘d listen to the radio Waiting for my favorite songs When they played I‘d sing along It make me smile Those were such happy times and not so long ago How I wondered where they‘d gone But they‘re back again just like a long lost friend All the songs I love so well Every sha la la la sha la la la every wo wo wo wo still shines Every sing aling aling sing aling aling that [[email protected] lamp]# grep "WWW*" abc.txt WWW [[email protected] lamp]# grep "WWWWW*" abc.txt [[email protected] lamp]#
举例 . 匹配任意单个字符
[[email protected] lamp]# grep "W..n" abc.txt 匹配以W开头n结尾中间任意单个字符内容。 When I was young I‘d listen to the radio Whon they played I‘d sing along [[email protected] lamp]#
举例 . 与 *的结合使用
[[email protected] lamp]# echo Wosddn >> abc.txt 先为文件abc.txt追加一段内容。 [[email protected] lamp]# grep "W.*n" abc.txt 过滤以W开头n结尾符合条件的内容。中间内容不限 When I was young I‘d listen to the radio Waiting for my favorite songs Whon they played I‘d sing along Wosddn [[email protected] lamp]# grep ".*" abc.txt 就是匹配abc.txt的所有内容 When I was young I‘d listen to the radio Waiting for my favorite songs Whon they played I‘d sing along It make me smile Those were such happy times and not so long ago How I wondered where they‘d gone But they‘re back again just like a long lost friend All the songs I love so well Every sha la la la sha la la la every wo wo wo wo still shines Every sing aling aling sing aling aling that WWW Wosddn
举例 ^ 匹配行首
[[email protected] lamp]# grep "^W" abc.txt 匹配以W开头的行 When I was young I‘d listen to the radio Waiting for my favorite songs Whon they played I‘d sing along WWW Wosddn [[email protected] lamp]#
举例 $ 匹配行尾
[[email protected] lamp]# grep "o$" abc.txt 匹配以o结尾的行 When I was young I‘d listen to the radio Those were such happy times and not so long ago [[email protected] lamp]#
举例 ^$结合使用
[[email protected] lamp]# echo >> abc.txt 输入一段空的内容追加到abc.txt文件中 [[email protected] lamp]# [[email protected] lamp]# grep -n "^$" abc.txt //-n显示过滤内容所在文件中的行号。 16: [[email protected] lamp]#
举例 [] 匹配中括号内指定的单个字符
[[email protected] lamp]# grep Wh[eo]n abc.txt 匹配指定单个e或o的字符 When I was young I‘d listen to the radio Whon they played I‘d sing along [[email protected] lamp]# [[email protected] lamp]# grep -n "[a-c]" abc.txt 匹配任意一个单字符abc的行 1:When I was young I‘d listen to the radio 2:Waiting for my favorite songs 3:Whon they played I‘d sing along 4:It make me smile 5:Those were such happy times and not so long ago 7:But they‘re back again just like a long lost friend 9:Every sha la la la 10:sha la la la every wo wo 12:Every sing aling aling 13:sing aling aling that [[email protected] lamp]#
举例 [^] 匹配出中括号的字符以外的任意一个字符
[[email protected] lamp]# grep -n "[^a-c]" abc.txt 匹配不包含单个字符abc的行 1:When I was young I‘d listen to the radio 2:Waiting for my favorite songs 3:Whon they played I‘d sing along 4:It make me smile999999 5:Those were such happy times and not so long ago 6:How I wondered where they‘d gone 8:But they‘re back again just like a long lost friend1234 9:All the songs I love so well 10:Every sha la la la 11:sha la la la every wo wo 12:wo wo still shines 14:Every sing aling aling 15:sing aling aling that 16:WWW 17:Wosddn 18:abc123 20:12345 21:9876qazwsx
举例 ^与[] 结合使用
[[email protected] lamp]# grep "^[a-z]" abc.txt 匹配小写字母a-z开头行 sha la la la every wo wo wo wo still shines sing aling aling that abc123 aa
举例 ^与[^] 结合使用
[[email protected] lamp]# grep "^[^a-zA-Z]" abc.txt 匹配不包含字母的行 12345 9876qazwsx 11111 111111111111111
举例 \ 转义符
[[email protected] lamp]# grep "\.$" abc.txt 由于“.”号具有匹配单个字符的能力,这地方转义成以普通字符。 Whon they played I‘d sing along. It make me smile999999. How I wondered where they‘d gone. All the songs I love so well. Every sha la la la.
举例 \{n\} 匹配前一个字符重复n次,\{n,\} 匹配前一个字符至少重复n次
[[email protected] lamp]# grep "W\{2\}" abc.txt 匹配字符W重复两次的行 WWW [[email protected] lamp]# grep "W\{2,\}" abc.txt WWW
由于正则是包含匹配所以\{n\} 与\{n,\}结果一样。只要掌握一种即可,当然极端情况下例外。
举例 \{n,m\} 匹配前一个字符重复n到m次
[[email protected] lamp]# grep "W\{1,3\}" abc.txt When I was young I‘d listen to the radio Waiting for my favorite songs Whon they played I‘d sing along WWW Wosddn [[email protected] lamp]# echo 11111 >> abc.txt [[email protected] lamp]# echo 111111111111111 >> abc.txt [[email protected] lamp]# [[email protected] lamp]# grep "1\{2,6\}" abc.txt 匹配字符1最少2个最多6个的行 11111 111111111111111 [[email protected] lamp]# grep "1\{6,10\}" abc.txt 匹配字符1最少6个最多10个的行 111111111111111 [[email protected] lamp]#
学习还是要多练习多用才能记牢,不要嫌烦嫌枯燥嫌练习简单就忽视不做。俗话说:熟能生巧,练多了自然记得牢悟的深。
本文出自 “NJ小生” 博客,请务必保留此出处http://liqingwen.blog.51cto.com/9853378/1916323
以上是关于shell学习之正则表达式基础篇的主要内容,如果未能解决你的问题,请参考以下文章