awk命令

awk用于操作文本文件中的列操作,下面为awk的一些常用操作

测试文件awkTest.txt,文本如下:

Bucks Milwaukee    60 22 0.732 
Raptors Toronto    58 24 0.707 
76ers Philadelphia 51 31 0.622
Celtics Boston     49 33 0.598
Pacers Indiana     48 34 0.585

1、输出所有列的值$0
[root@localhost shellTest]# awk '{print $0}' awkTest.txt

#执行结果
Bucks Milwaukee 60 22 0.732 
Raptors Toronto 58 24 0.707 
76ers Philadelphia 51 31 0.622
Celtics Boston 49 33 0.598
Pacers Indiana 48 34 0.585
2、输出第3列的值$3
[root@localhost shellTest]# awk '{print $3}' awkTest.txt

#执行结果
60 58 51 49 48

 3、输出内容包含0.5的行:包含内容使用双斜线//包含起来

[root@localhost shellTest]# awk '/0.5/ {print $0}' awkTest.txt 
#执行结果
Celtics Boston     49 33 0.598
Pacers Indiana     48 34 0.585

 4、输出数字开头的记录

[root@localhost shellTest]# awk '/^[0-9]/ {print $0}' awkTest.txt 
#执行结果
76ers Philadelphia 51 31 0.622

 5、输出第二个字段包含ia的记录(包含使用~表示)

awk '$2 ~/ia/ {print $0}' awkTest.txt 
#执行结果
76ers Philadelphia 51 31 0.622
Pacers Indiana     48 34 0.585

6、输出第三个字段≥50的记录

[root@localhost shellTest]# awk '$3 >=50 {print $0}' awkTest.txt 
#执行结果
Bucks Milwaukee    60 22 0.732 
Raptors Toronto    58 24 0.707 
76ers Philadelphia 51 31 0.622

 

 

 



posted @ 2020-11-04 11:22  雨后的太阳  阅读(128)  评论(0编辑  收藏  举报