欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

awk命令使用

程序员文章站 2022-04-30 14:46:41
...

用法一:

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

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

用法二:

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

[[email protected] riskbell]# cat 3.txt 
a,1,q
b,2,w
c,3,e
d,4,r
[[email protected] 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 # 设置变量

[[email protected] riskbell]# cat 2.txt 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
[[email protected] 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