本文最后更新于 1333 天前,其中的信息可能已经有所发展或是发生改变。
[TOC]
正则表达式
分类 | |
---|---|
基础正则 | ^ $ ^$ . * [a-z] [^abc] |
扩展正则 | + |() {} ? |
正则和通配符
分类 | |
---|---|
正则 | 高级语言、过滤字符、配合sed、grep、awk使用 |
通配符(glob) | 匹配文件名、匹配文件 |
基础正则
Teach Linux
Like playing basketball and learning
My blog is: https://www.2331314.xyz/
My github is: https://github.com/limitrinno
My qq is 1250644268
My WeChat is weixinhaomajiade
A string of meaningless numbers 0998563245
Good idea, let's start learning now
基础正则 | |
---|---|
^ | |
^ 以.....开头的行
- ^github 以github开头的行
- ^My 以My开头的行
$ 以……结尾的行
- cat -A 可以看到隐藏的行
- z $ 可以看到以z结尾的行
…^$ 匹配空行
- 匹配内容为空的行数
- 空格也算是一个字符
匹配所有的空格
[root@localhost 2]# grep ' ' clc.txt
Teach Linux
Like playing basketball and learning
My blog is: https://www.2331314.xyz
My docs is: https://docs.2331314.xyz
My github is: https://github.com/limitrinno
My qq is 1250644268
My WeChat is weixinhaomajiade
A string of meaningless numbers 0998563245
Good idea, let's start learning now
匹配所有的空行 -n
是为了显示行数
[root@localhost 2]# grep '^$' -n clc.txt
3:
7:
10:
^ $实例
- 排除文中的空行 (grep -v '^$' clc.txt)
[root@localhost 2]# grep -v '^$' clc.txt
Teach Linux
Like playing basketball and learning
My blog is: https://www.2331314.xyz
My docs is: https://docs.2331314.xyz
My github is: https://github.com/limitrinno
My qq is 1250644268
My WeChat is weixinhaomajiade
A string of meaningless numbers 0998563245
Good idea, let's start learning now
- 排除文中#开头的行
[root@localhost 2]# cat a.txt
Teach Linux
Like playing basketball and learning
My blog is: https://www.2331314.xyz
My docs is: https://docs.2331314.xyz
My github is: https://github.com/limitrinno
#My github is: https://github.com/limitrinno
My qq is 1250644268
My WeChat is weixinhaomajiade
#My WeChat is weixinhaomajiade
A string of meaningless numbers 0998563245
Good idea, let's start learning now
[root@localhost 2]# grep -v '^#' a.txt
Teach Linux
Like playing basketball and learning
My blog is: https://www.2331314.xyz
My docs is: https://docs.2331314.xyz
My github is: https://github.com/limitrinno
My qq is 1250644268
My WeChat is weixinhaomajiade
A string of meaningless numbers 0998563245
Good idea, let's start learning now