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
复制代码

 

posted @   小鲨鱼2018  阅读(1366)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示