扩展正则表达式元字符
+:匹配前面子表达式1次以上
例如:go+d,将匹配至少一个o(如god,good,goood…)
?:匹配前面子表达式0次或者1次
例如:go?d,将匹配gd或god
():括号中的字符串作为一个整体
例如:(xyz)+,将匹配xyz整体1次以上,如xyzxyz
|:以或的方式匹配字条串
例如1:good|food,将匹配good或者food
例如2:g(oo|la)d,将匹配good或者glad
(g|f)ood good或者food则表达式
2 egrep
使用方法
示例
查询"wood" “woood” “wooood"等字符串
egrep -n ‘wo+d’ test.txt
查询"bet” “best” 这两个字符串
egrep -n ‘bes?t’ test.txt
查询"of" 或"if"或"on" 字符串
egrep -n ‘of|if|on’ test.txt
查询"tast" “test” 这两个字符串
egrep -n ‘t(a|e)st’ test.txt
awk
3.1 格式
awk 选项 ‘模式或条件{编辑命令}’ 文件1 文件2…. //过滤并输出文件中符合条件的内容
awk -f 脚本文件 文件1 文件2… //从脚本中调用的编辑指令,过滤并输出内容
##3.3 示例
按行输出
1 输出所有内容,等同于cat test.txt
awk ‘{print}’ test.txt
2 输出所有内容,等同于cat test.txt
awk ‘{print $0}’ test.txt
3 输出第1~3行内容
awk ‘NR1,NR3{print}’ test.txt
4 输出第1~3行内容
awk ‘(NR>=1)&&(NR<=3){print}’ test.txt
5 输出第1行,第3行的内容
awk ‘NR1||NR3{print}’ test.txt
按字段输出
1 输出每行中(以空格或制表符分隔)的第三个字段
awk ‘{print $3}’ test.txt
2 输出每行中第1,3个字段
awk ‘{print $1,$3}’ test.txt
3 输出密码为空的用户的shadow记录
awk -F: ‘$2=="!!"{print}’ /etc/shadow
4 输出密码为空的用户的shadow记录
awk ‘BEGIN {FS=":"} ; $2=="!!"{print}’ /etc /shadow
5 输出以冒号分隔且第7个字段中包含/bash的行的第一个字段
awk -F: ‘$7~"/bash"{print $1}’ /etc/passwd