Vicky
Vicky

Reputation: 1328

Finding operation time using awk

I have a file like below

RequestID=654478
Loginfo:ServerXX
Error:NA
Task=OPERATION1
File:F1
User:a
Time:3.12 ms
Network:AVL
RequestID=122644
Loginfo:ServerX2
Error:Mem
Task=OPERATION2
File:F11
User:b
Time=31.33 ms
Network:AVL
RequestID=324563
Loginfo:ServerX1
Error:Net
Task=OPERATION2
File:F1
User:c
Time=30.01 ms
Network:AVL

I want to find time and RequestIDs for OPERATION2

so Output should be:

RequestID=122644   Time=31.33 ms
RequestID=324563   Time=30.01 ms

I am able to find solution using combination of linux commands but How do I do it with awk or sed ?

Upvotes: 0

Views: 46

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

GNU Awk solution:

awk '/^RequestID=/{ r_id=$1 }r_id && /^Task=OPERATION2\>/{ op=$1 }
     op && /Time=/{ printf "%s\t%s\n", r_id, $0;  r_id=op="" }' file
  • /^Task=OPERATION2\>/ - matches line with OPERATION2, where \> is a word boundary ensures that only OPERATION2 will be matched but not OPERATION22

The output:

RequestID=122644    Time=31.33 ms
RequestID=324563    Time=30.01 ms

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203655

$ cat tst.awk
BEGIN { FS="[=:]"; OFS="\t" }
{ f[$1] = $0 }
$1 == "Network" {
    if ( f["Task"] ~ /=OPERATION2$/ ) {
        print f["RequestID"], f["Time"]
    }
}

$ awk -f tst.awk file
RequestID=122644        Time=31.33 ms
RequestID=324563        Time=30.01 ms

Upvotes: 2

Related Questions