解析用awk大的测试文件 [英] Parse the large test files using awk

查看:153
本文介绍了解析用awk大的测试文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待用awk解析一个空格分隔输入的文本文件。列code可以具有用于每个组的多个行。我将不胜AP preciate任何帮助。

输入文件:

  TR 1
行动成功/失败
8.1.1.1 RunOne 80 48
8.1.1.2 RunTwo 80 49
8.1.1.3 RunThree 100 100
8.1.1.4 RunFour 20 19
8.1.1.5 RunFive 20 20
动作时间16时47分42秒
措施2成功/失败
8.1.2.1 RunSix 80 49
8.1.2.2 RunSeven 80 80
8.1.2.3 RunEight 80 80
措施2时间三时26分31秒TR 2
行动成功/失败
8.1.1.1 RunOne 80 48
8.1.1.2 RunTwo 80 49
8.1.1.3 RunThree 100 100
8.1.1.4 RunFour 20 19
8.1.1.5 RunFive 20 20
动作时间16时47分42秒
措施2成功/失败
8.1.2.1 RunSix 80 49
8.1.2.2 RunSeven 80 80
8.1.2.3 RunEight 80 80
措施2时间三时26分31秒

所需的输出文件

  ------------------
s.no Runno Runname VAL1将val2%VAL1&安培;将val2
1. 8.1.1.1 Runone 160 96%#VAL1和val2应显示为TR1和放大器的总和; TR2
2. 8.1.1.2 Runtwo 160 98
3. 8.1.1.3 Runthree 200 200
4. 8.1.1.4 RunFour 40 38
.......

和还没有找到从每个TR 1(TestRun)

每个Runname的出现

code低于

 #!的/ usr /斌/的awk -f开始 {
    #您可以自定义此基础上您的preference改变你的输出布局。
    格式=%-10s%-7s%-5S%-8S \\ n
    printf格式,Runno,Runname,VAL1,VAL2
}
++我== 2 {
    L = $ 1
}
我大于100 {
    如果(/ ^ [[:空白:]] * $ /){
        I = 0
    }否则如果(NF→1){
        printf格式,L,$ 1,$ 2,$ 3,$ 4,$ 5
        P1 = $ 1; P2 = $ 2; P3 = $ 3; P5 = $ 5
    }其他{        printf格式,L,P1,P2,P3,$ 1,P5
    }
}


解决方案

这可能会给你一个起点。 注意:输出没有排序,缺少的头部,并从输出的第一列,但我要离开了你。

 的awk'{/^8\\./一个[$ 1$ 2] + = $ 3; B〔$ 1$ 2] + = $ 4}
     END {为(在K){
             的printf(%s%I%I \\ N,K,A [K],B [K])
         }
    }'INPUTFILE


  1. / ^ 8 \\ ./ 它的相关线路只工作

  2. A [$ 1$ 2] + = $ 3; B〔$ 12 $] + = $ 4'/ code>的相关线商店并递增第三和第四列

  3. 终于通过了 A 循环访问数组打印的相关数据,并从 B 阵列获取数据,以及

I am looking to parse a space delimited input text file using awk. The column code can have more than one row for each group. I would greatly appreciate any help with this.

Input File:

TR 1   
Action   

Success/Failure 
8.1.1.1   RunOne   80   48
8.1.1.2   RunTwo   80   49
8.1.1.3   RunThree   100   100
8.1.1.4   RunFour   20   19
8.1.1.5   RunFive   20   20
Action Time   16:47:42
Action2   

Success/Failure 
8.1.2.1   RunSix   80   49
8.1.2.2   RunSeven   80   80
8.1.2.3   RunEight   80   80
Action2 Time   03:26:31

TR 2    
Action

Success/Failure
8.1.1.1   RunOne   80   48
8.1.1.2   RunTwo   80   49
8.1.1.3   RunThree   100   100
8.1.1.4   RunFour   20   19
8.1.1.5   RunFive   20   20
Action Time   16:47:42
Action2   

Success/Failure 
8.1.2.1   RunSix   80   49
8.1.2.2   RunSeven   80   80
8.1.2.3   RunEight   80   80
Action2 Time   03:26:31

Desired output file

------------------
s.no  Runno   Runname val1 val2  %val1&val2
1.    8.1.1.1 Runone  160  96    %       #val1 and Val2 should display as sum of TR1&TR2
2.    8.1.1.2 Runtwo  160  98
3.    8.1.1.3 Runthree 200 200
4.    8.1.1.4 RunFour 40 38
....... 

and also find no of occurrences of Each Runname from each TR 1(TestRun)

Code is below

#!/usr/bin/awk -f

BEGIN {
    # You can customize this to change your output layout based on your preference.
    format = "%-10s%-7s%-5s%-8s\n"
    printf format, "Runno", "Runname", "Val1", "Val2"
}
++i==2{
    l = $1
}
i>100{
    if (/^[[:blank:]]*$/) {
        i = 0
    } else if (NF > 1) {
        printf format, l, $1, $2, $3, $4, $5
        p1=$1; p2=$2; p3=$3; p5=$5
    } else {

        printf format, l, p1, p2, p3, $1, p5
    }
}

解决方案

This might give you a starting point. Note: the output is not sorted, and missing the header and the first column from the output, but I'm leaving that to you!

awk '/^8\./ { a[$1 " " $2] += $3 ; b[$1 " " $2] += $4 } 
     END { for (k in a) { 
             printf("%s %i %i\n",k,a[k],b[k]) 
         } 
    }' INPUTFILE

  1. With /^8\./ it work only on the relevant lines
  2. a[$1 " " $2] += $3 ; b[$1 " " $2] += $4 for the relevant lines store and increment the third and fourth columns
  3. finally print the relevant data by iterating through the a array and get the data from the b array as well.

这篇关于解析用awk大的测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆