元字符

  • . 匹配除换行符外的任意单个字符
  • * 匹配任意一个跟在它前面的字符
  • [] 匹配方括号中的字符类中的任意一个
  • ^ 匹配开头
  • $ 匹配结尾
  • \ 转义后面的特殊字符

举个例子,现在先准备一个文件 a.txt ,内容如下:

  1. @ root password
  2. a.txt
  3. es passarld=123

使用 grep + 元字符查找文本:

# 完整匹配
[root@liyufei ~]# grep password a.txt 
@ root password

# 以pass开头,后面匹配四个字符
[root@liyufei ~]# grep pass.... a.txt
@ root password
es passarld=123

# 以pass开头,后面匹配四个字符,并以此为结束
[root@liyufei ~]# grep pass....$ a.txt
@ root password

# 匹配任意,没啥用处
[root@liyufei ~]# grep . a.txt 
@ root password
a.txt
es passarld=123

# * 通常和 . 配合使用,意为以 pass 开头的任意字符串
[root@liyufei ~]# grep pass.*$ a.txt 
@ root password
es passarld=123

# []为匹配或者的关系
[root@liyufei ~]# [Hh]ello Hello hello

# 匹配以 @ 开头的文本
[root@liyufei ~]# grep ^@ a.txt 
@ root password

# 转义字符,比如真的只是查询 . 匹配的文本,同时注意加上双引号,避免shell转义
[root@liyufei ~]# grep "\." a.txt 
a.txt

find命令

查找各种文件

有时间需要把这部分在看下补充一下

sed命令

对文本内容做替换各种操作

有时间需要把这部分在看下补充一下

awk命令

对文本内容做统计,更像是编程语言之类的

有时间需要把这部分在看下补充一下