Appearance
GAWK
注意
下列命令示例均基于 Linux 平台。
输出时间
$ echo | awk '{ printf("TIME: %s\n", strftime("%Y-%m-%d %H:%M:%S", systime())) }'
TIME: 2021-11-09 11:59:06
$ echo | awk '{ printf("TIME: %s\n", strftime("%Y-%m-%d %H:%M:%S", systime())) }'
TIME: 2021-11-09 11:59:06
正则替换字符
$ cat a
time: 2011-11-04 value: 695.59MB
time: 2011-11-05 value: 697.75MB
time: 2011-11-06 value: 701.25MB
time: 2011-11-07 value: 711.00MB
time: 2011-11-08 value: 718.88MB
$ awk '{gsub("MB", ""); print}' a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75
time: 2011-11-06 value: 701.25
time: 2011-11-07 value: 711.00
time: 2011-11-08 value: 718.88
$ cat a
time: 2011-11-04 value: 695.59MB
time: 2011-11-05 value: 697.75MB
time: 2011-11-06 value: 701.25MB
time: 2011-11-07 value: 711.00MB
time: 2011-11-08 value: 718.88MB
$ awk '{gsub("MB", ""); print}' a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75
time: 2011-11-06 value: 701.25
time: 2011-11-07 value: 711.00
time: 2011-11-08 value: 718.88
计算行数 (类似于 wc -l
)
$ cat a1
time: 2011-11-04 value: 695.59MB
time: 2011-11-05 value: 697.75MB
$ awk 'END {print NR " " FILENAME}' a1
2 a1
$ wc -l a1
2 a1
$ cat a1
time: 2011-11-04 value: 695.59MB
time: 2011-11-05 value: 697.75MB
$ awk 'END {print NR " " FILENAME}' a1
2 a1
$ wc -l a1
2 a1
以文件为单位,给文件内的每一行增加编号
$ awk '{print FILENAME "\t" FNR "\t" $0}' a*
a1 1 time: 2011-11-04 value: 695.59MB
a1 2 time: 2011-11-05 value: 697.75MB
a2 1 time: 2011-11-06 value: 701.25MB
a2 2 time: 2011-11-07 value: 711.00MB
a2 3 time: 2011-11-08 value: 718.88MB
$ awk '{print FILENAME "\t" FNR "\t" $0}' a*
a1 1 time: 2011-11-04 value: 695.59MB
a1 2 time: 2011-11-05 value: 697.75MB
a2 1 time: 2011-11-06 value: 701.25MB
a2 2 time: 2011-11-07 value: 711.00MB
a2 3 time: 2011-11-08 value: 718.88MB
以行为单位,给所有文件增加编号
$ awk '{print FILENAME "\t" NR "\t" $0}' a*
a1 1 time: 2011-11-04 value: 695.59MB
a1 2 time: 2011-11-05 value: 697.75MB
a2 3 time: 2011-11-06 value: 701.25MB
a2 4 time: 2011-11-07 value: 711.00MB
a2 5 time: 2011-11-08 value: 718.88MB
$ awk '{print FILENAME "\t" NR "\t" $0}' a*
a1 1 time: 2011-11-04 value: 695.59MB
a1 2 time: 2011-11-05 value: 697.75MB
a2 3 time: 2011-11-06 value: 701.25MB
a2 4 time: 2011-11-07 value: 711.00MB
a2 5 time: 2011-11-08 value: 718.88MB
相邻行的指定列相减
$ cat a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75
time: 2011-11-06 value: 701.25
time: 2011-11-07 value: 711.00
time: 2011-11-08 value: 718.88
$ awk 'NR==1 { tmp=$NF } NR>1 { print $NF-tmp; tmp=$NF }' a
2.16
3.5
9.75
7.88
$ awk 'NR==1 { print $0; tmp=$NF } NR>1 { printf("%s\tvalue_chg: %s\n", $0, $NF-tmp); tmp=$NF }' a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75 value_chg: 2.16
time: 2011-11-06 value: 701.25 value_chg: 3.5
time: 2011-11-07 value: 711.00 value_chg: 9.75
time: 2011-11-08 value: 718.88 value_chg: 7.88
$ cat a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75
time: 2011-11-06 value: 701.25
time: 2011-11-07 value: 711.00
time: 2011-11-08 value: 718.88
$ awk 'NR==1 { tmp=$NF } NR>1 { print $NF-tmp; tmp=$NF }' a
2.16
3.5
9.75
7.88
$ awk 'NR==1 { print $0; tmp=$NF } NR>1 { printf("%s\tvalue_chg: %s\n", $0, $NF-tmp); tmp=$NF }' a
time: 2011-11-04 value: 695.59
time: 2011-11-05 value: 697.75 value_chg: 2.16
time: 2011-11-06 value: 701.25 value_chg: 3.5
time: 2011-11-07 value: 711.00 value_chg: 9.75
time: 2011-11-08 value: 718.88 value_chg: 7.88