测试开发进阶(四十二)

测试开发进阶(四十二)

awk

文本处理工具,处理数据并生成结果报告

  • awk ‘BEGIN{}pattern{commands}END{}’ file
  • some command | awk ‘BEGIN{}pattern{commands}END{}’

BEGIN处理数据之前执行

pattern匹配模式

commands处理的命令

END处理数据之后执行

内置变量

  • $0整行内容
  • $1~$n当前行的第1~n个字段
  • NF当前行字段数
  • NR当前行号,从1开始
  • FS输入字段分割符,默认为空格或tab键
  • RS输入行分割符,默认为回车符
  • OFS输入字段分割符,默认为空格
  • ORS输入行分割符,默认为回车符

输出以:分割的最后一列内容

1
$ awk 'BEGIN{FS=":"} {print $NF}' passwd

输出以:分割的games开头的行到new开头的行的最后一列内容

1
$ awk 'BEGIN{FS=":"}/^games/,/^new/{print $NF}' passwd

printf格式符

  • %s字符串
  • %d十进制数字
  • %f浮点数
  • +右对齐
  • -左对齐

%s\n的格式输出

1
2
3
$ awk 'BEGIN{FS=":"}/^games/,/^new/{printf "%s\n" , $1}' passwd
$ awk 'BEGIN{FS=":"}/^games/,/^new/{printf "%20s\n" , $1}' passwd
$ awk 'BEGIN{FS=":"}/^games/,/^new/{printf "%20s %s\n" , $1,$NF}' passwd

awk1

1
$ awk 'BEGIN{FS=":"}/^games/,/^new/{printf "%-20s\t%-20s\n" , $1,$NF}' passwd

awk2

格式化输出以s开头的行好和第一列内容

1
$ awk 'BEGIN{FS=":"}/^s/{printf "%d\t%-10s\n" , NR,$1}' passwd

awk3

格式化输出第三例大于100的内容

1
$ awk -F ":" '$3 > 100 {printf "%-10s\t%-10s\n",$1,$(NF-1)}' passwd 

awk4

添加头部和尾部

1
$ awk -F ":" 'BEGIN{printf "%-10s\t%-10s\n","username","path"}$3 > 100 {COUNT++;printf "%-10s\t%-10s\n",$1,$(NF-1)}END{printf "%-10s\t%-10s\n","totally",COUNT}' passwd 

awk5

使用文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# awk1.awk
BEGIN{
printf "%-20s\t%-20s\n","UserName","Path"
}
{
if ($3 > 100){
COUNT++
printf "%-20s\t%-20s\n",$1,$(NF-1)
}
}
END{
printf "%-20s\t%-20s\n","Totally",COUNT
}
1
$ awk -F ':' -f awk1.awk  passwd 

awk6

日志分析

日志文件

获取每个测试人员执行的用例数

1
2
3
4
5
6
7
8
9
10
11
BEGIN{
printf "%-10s%-10s\n","Tester","TotalTestcases"
}
{
USERS[$4] += 1
}
END{
for (u in USERS){
printf "%-10s%-10s\n",u,USERS[u]
}
}
1
$ awk -f 1201.awk testcases.log 

文件处理1

统计每位测试人员执行用例成功和失败的总数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BEGIN{
printf "%-10s%-12s%-12s\n","Tester","PassTotal","FailTotal"
}
{
if ($5 == "Pass"){
SUCCESS[$4] += 1
}
else if($5 == "Fail"){
FAIL[$4] += 2
}
USERS[$4] += 1
}
END{
for (u in USERS){
printf "%-10s%-12s%-12s\n",u,SUCCESS[u],FAIL[u]
}
}
1
$ awk -f 1201_2.awk testcases.log 

文件处理2

同时分别统计每位测试人员执行用例CRITICAL、ERROR日志等级数以及所有测试人员每项总数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
BEGIN{
printf "%-10s%-12s%-12s%-12s%-12s\n","Tester","PassTotal","FailTotal","CRITICALATotal","ERRORTotal"
}
{
if ($5 == "Pass"){
SUCCESS[$4] += 1
s += 1
}
else if($5 == "Fail"){
FAIL[$4] += 1
f += 1
}
if($3 == "CRITICAL"){
CRITICAL[$4] += 1
c += 1
}
else if($3 == "ERROR"){
ERROR[$4] += 1
e += 1
}
USERS[$4] += 1
}
END{
for (u in USERS){
printf "%-10s%-12s%-12s%-12s%-12s\n",u,SUCCESS[u],FAIL[u],CRITICAL[u],ERROR[u]
}
printf "%-10s%-12s%-12s%-12s%-12s\n","Total",s,f,c,e
}

文件处理3

使用shell脚本来实现,并显示菜单,选择不同的选项则打印不同的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
#Author:zhongxin
echo "one:统计每位测试人员执行用例的总数"
echo "two:分别统计每位测试人员执行用例成功和失败的总数"
echo "three:同时分别统计每位测试人员执行用例CRITICAL、ERROR日志等级数以及所有测试人员每项总数"
read -p "请输入选择的模式:" modes
echo ${modes}
case ${modes} in
one)
awk -f ./1201.awk testcases.log
;;
two)
awk -f ./1201_2.awk testcases.log
;;
three)
awk -f ./1201_3.awk testcases.log
;;
*)
echo "请输入正确的模式"
;;
esac
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!