linux系统中awk命令 正则匹配


 

1、测试数据

[root@centos7 test3]# cat b.txt
e t s e
s g m x
w d g i
d t e g
x g e w

 

2、打印匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '/w/' b.txt w d g i x g e w [root@centos7 test3]# awk '/w/ {print $1, $3}' b.txt w g x e

 

3、打印第一列匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$1 ~ /w/' b.txt w d g i [root@centos7 test3]# awk '$1 ~ /w/ {print $1,$4}' b.txt w i

 

4、打印全文没有匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '!/w/' b.txt e t s e s g m x d t e g

 

5、打印第一列没有匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$1 !~ /w/' b.txt e t s e s g m x d t e g x g e w

 

6、打印同时匹配s或者w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '/[sw]/' b.txt e t s e s g m x w d g i x g e w

 

7、打印第3列匹配e同时第四列匹配g的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$3 ~ /e/' b.txt d t e g x g e w [root@centos7 test3]# awk '$3 ~ /e/ && $4 ~ /g/' b.txt d t e g

 

8、输出第3列没有匹配e的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$3 !~ /e/' b.txt e t s e s g m x w d g i

 

9、匹配包含两个关键词中间的所有行

[root@centos7 test3]# cat c.txt 1 张三 历史 81 B 0.367
2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571
1 张三 历史 81 B 0.367
2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571 [root@centos7 test3]# awk '/李四/,/陈明/' c.txt 2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812

 

[root@centos7 test3]# cat a.txt 1 张三 历史 81 B 0.367
2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876
5 陈明 语文 81 B 0.812
6 鱼鱼 英语 81 B 0.571 [root@centos7 test3]# awk '$4==72,$4==91 {print $0}' a.txt 2 李四 物理 72 C 0.588
3 李华 数学 87 B+ 0.677
4 方咪 历史 91 A 0.876

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM