awk命令使用

用法一:

awk '{[pattern] action}' {filenames} # 行匹配语句 awk '' 只能用单引号
实例

[root@k8s-uat-node73 riskbell]# cat 2.txt 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
[root@k8s-uat-node73 riskbell]# awk '{print $1,$4}' 2.txt
2 a
3 like
This's 
10 orange,apple,mongo
# 格式化输出
[root@k8s-uat-node73 riskbell]# awk '{printf "%-8s %-10s\n",$1,$4}' 2.txt
2        a         
3        like      
This's             
10       orange,apple,mongo

用法二:

awk -F #-F相当于内置变量FS, 指定分割字符

[root@k8s-uat-node73 riskbell]# cat 3.txt 
a,1,q
b,2,w
c,3,e
d,4,r
[root@k8s-uat-node73 riskbell]# awk -F, '{print $1,$2}'   3.txt
a 1
b 2
c 3
d 4
# 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
 $ awk -F '[ ,]'  '{print $1,$2,$5}'   log.txt

用法三:

awk -v # 设置变量

[root@k8s-uat-node73 riskbell]# cat 2.txt 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
[root@k8s-uat-node73 riskbell]# awk -va=1 '{print $1,$1+a}' 2.txt 
2 3
3 4
This's 1
10 11

http://www.runoob.com/linux/linux-comm-awk.html

你可能感兴趣的:(awk命令使用)