user3745740
user3745740

Reputation: 7

Parse the large test files using awk

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
    }
}

Upvotes: 1

Views: 172

Answers (1)

Zsolt Botykai
Zsolt Botykai

Reputation: 51653

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.

Upvotes: 1

Related Questions