Linux Shell 三剑客之一,废话不多说直接上干货。
目录
- 1. 指定分隔符
- 2.指定打印行数
- 3.打印最后一列
- 4.打印倒数第二列
- 5.匹配输出
- 6.if 判断
- 7.统计列总数值
- 8.时间格式转换
示例数据:
1. 指定分隔符
awk -F '|' '{print $1}' log.txt
# awk -F '|' '{print $1}' log.txt
tbname
=================================================================
tb_7456_cm1eM
tb_7456_cm1e0
tb_7456_1bg3J
tb_7456_1dXax
tb_7456_1b9ol
tb_7456_1bhdH
tb_7456_1eAYV
tb_7456_1fxkg
tb_7456_1fZRW
tb_7456_cm1ez
tb_7456_1bg3J
tb_7456_cm1hX
tb_7456_cm1ck
tb_7456_cm1ck
tb_7456_cm1ck
2.指定打印行数
awk -F '|' 'NR>2 {print $1}' log.txt
# awk -F '|' 'NR>2 {print $1}' log.txt
tb_7456_cm1eM
tb_7456_cm1e0
tb_7456_1bg3J
tb_7456_1dXax
tb_7456_1b9ol
tb_7456_1bhdH
tb_7456_1eAYV
tb_7456_1fxkg
tb_7456_1fZRW
tb_7456_cm1ez
tb_7456_1bg3J
tb_7456_cm1hX
tb_7456_cm1ck
tb_7456_cm1ck
tb_7456_cm1ck
3.打印最后一列
awk -F '|' 'NR>2 {print $NF}' log.txt
# awk -F '|' 'NR>2 {print $NF}' log.txt
1532
13
209
56556
3334
7611
48109
3
1668
7249
145
22
6
1
506
4.打印倒数第二列
awk -F '|' 'NR>2 {print $(NF-1)}' log.txt
# awk -F '|' 'NR>2 {print $(NF-1)}' log.txt
108
350
293
57
207
316
26
27
148
203
18
480
64
20
348
5.匹配输出
awk -F '|' '/cm1e/ {print $1}' log.txt
# awk -F '|' '/cm1e/ {print $1}' log.txt
tb_7456_cm1eM
tb_7456_cm1e0
tb_7456_cm1ez
6.if 判断
awk -F '|' '{if ($3<100) print $0 }' log.txt
# awk -F '|' '{if ($3<100) print $0 }' log.txt
tbname | create_time | cols| rows
=================================================================
tb_7456_1dXax | 2023-03-21 05:38:44.400 | 57 | 56556
tb_7456_1eAYV | 2023-03-21 03:50:16.347 | 26 | 48109
tb_7456_1fxkg | 2023-03-21 03:50:15.484 | 27 | 3
tb_7456_1bg3J | 2023-09-05 12:42:18.269 | 18 | 145
tb_7456_cm1ck | 2023-09-05 02:03:43.843 | 64 | 6
tb_7456_cm1ck | 2023-03-21 03:50:19.610 | 20 | 1
# awk -F '|' '/cm1/ {if ($3<100) print $1"\t"$3 }' log.txt
tb_7456_cm1ck 64
tb_7456_cm1ck 20
7.统计列总数值
awk -F '|' 'NR>2 {sum+=$3} END {print sum}' log.txt
# awk -F '|' 'NR>2 {sum+=$3} END {print sum}' log.txt
2665
8.时间格式转换
awk '{print strftime("%Y-%m-%d %T",1697848286)}'
# date +%s |awk '{print strftime("%Y-%m-%d %T",$1)}'
2023-10-21 08:32:44