Linux awk使用

awk是一种编程语言,用于在linux/unix下对文本和数据进行处理。

awk命令格式和选项

语法形式

awk [options] ‘script’ var=value file(s)
awk [options] -f script_file var=value file(s)

常用命令选项

  • -F fs: fs指定输入分隔符, fs可以是字符串或正则表达式,如-F:
  • -v var=value 赋值一个用户定义变量,将外部变量传递给awk
  • -f script_file 从脚本文件中读取awk命令

基本用法

  • log.txt文本内容如下:
1
2
3
4
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo

用法一:

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

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
# 每行按空格或TAB分割,输出文本中的1、4项
[root@caoxl ~]# awk '{print $1,$4}' log.txt
2 a
3 like
This's
10 orange,apple,mongo

# 格式化输出
[root@caoxl ~]# awk '{printf "%-8s %-10s\n",$1,$4}' log.txt
2 a
3 like
This's
10 orange,apple,mongo

用法二:

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

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 使用","分割
[root@caoxl ~]# awk -F, '{print $1,$2}' log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange apple

# 或者使用内建变量
[root@caoxl ~]# awk 'BEGIN{FS=","}{print $1,$2}' log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange apple

# 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
2 this
3 Are
This's a
10 There

用法三:

1
awk -v # 设置变量

实例

1
2
3
4
5
6
7
8
9
10
11
[root@caoxl ~]# awk -va=1 '{print $1,$1+a}' log.txt
2 3
3 4
This's 1
10 11

[root@caoxl ~]# awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt
2 3 2s
3 4 3s
This's 1 This'ss
10 11 10s

用法四:

1
awk -f {awk脚本} {文件名}

实例

  • cal.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/awk -f
#运行前
BEGIN {
math = 0
english = 0
computer = 0

printf "NAME NO. MATH ENGLISH COMPUTER TOTAL\n"
printf "---------------------------------------------\n"
}
#运行中
{
math+=$3
english+=$4
computer+=$5
printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3, $4, $5, $3+$4+$5
}
#运行后
END {
printf "---------------------------------------------\n"
printf " TOTAL:%10d %8d %8d \n", math, english, computer
printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR
}
  • score.txt
1
2
3
4
5
Marry   2143 78 84 77
Jack 2321 66 78 45
Tom 2122 48 77 71
Mike 2537 87 97 95
Bob 2415 40 57 62
  • 运行
1
2
3
4
5
6
7
8
9
10
11
[root@caoxl ~]# awk -f cal.awk score.txt
NAME NO. MATH ENGLISH COMPUTER TOTAL
---------------------------------------------
Marry 2143 78 84 77 239
Jack 2321 66 78 45 189
Tom 2122 48 77 71 196
Mike 2537 87 97 95 279
Bob 2415 40 57 62 159
---------------------------------------------
TOTAL: 319 393 350
AVERAGE: 63.80 78.60 70.00

其他用法:

  • 从文件中找出长度大于30的行
1
2
[root@caoxl ~]# awk 'length>30' log.txt 
10 There are orange,apple,mongo
  • 打印九九乘法表
1
2
3
4
5
6
7
8
9
10
[root@caoxl ~]# seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2022 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :