xergiopd
xergiopd

Reputation: 27

Shell script - Regroup output

A log file yelds the following output:

PROGRAM             TYPE           TOT  DATE        TIME
X Attribute Stat : Type Error N114  1   20140521    0012
Y Attribute Stat : Type Error N115  4   20140521    0012
Y Attribute Stat : Type Error N118  1   20140521    0012
X Attribute Stat : Type Error N119  2   20140521    0012
X Attribute Stat : Type Error N122  1   20140521    0012
X Attribute Stat : Type Error N123  1   20140521    0115
X Attribute Stat : Type Error N124  1   20140521    0115
X Attribute Stat : Type Error N125  1   20140521    0115
X Attribute Stat : Type Error N126  3   20140521    0115
X Attribute Stat : Type Error N26   1   20140521    0154
Y Attribute Stat : Type Error N17   36  20140521    0456
Y Attribute Stat : Type Error N9    13  20140521    0457
Y Attribute Stat : Type Error N17   2   20140521    0457
X Attribute Stat : Type Error N6    4   20140521    1138
X Attribute Stat : Type Error N16   1   20140521    1138
X Attribute Stat : Type Error N17   7   20140521    1138
X Attribute Stat : Type Error N18   2   20140521    1138
X Attribute Stat : Type Error N19   2   20140521    1138
X Attribute Stat : Type Error N21   33  20140521    1138
X Attribute Stat : Type Error N24   5   20140521    1138
X Attribute Stat : Type Error N25   10  20140521    1138
X Attribute Stat : Type Error N27   1   20140521    1138
X Attribute Stat : Type Error N28   1   20140521    1138
Y Attribute Stat : Type Error N29   2   20140521    1138
Y Attribute Stat : Type Error N30   1   20140521    1138
Y Attribute Stat : Type Error N32   1   20140521    1138
X Attribute Stat : Type Error N108  12  20140521    1210

i need to parse it and regroup the total number of errors by time, and the output of the script will be:

                TIME    TOT
X Attribute     0012    4
Y Attribute     0012    5
X Attribute     0115    6
X Attribute     0154    1
Y Attribute     0456    36
Y Attribute     0457    15
X Attribute     1138    66
Y Attribute     1138    4
X Attribute     1210    12

What's a performant solution with a bash script to achieve the following output?

Upvotes: 1

Views: 91

Answers (3)

Vijay
Vijay

Reputation: 67211

awk 'NR!=1{a[$1" "$2" "$10]+=$8}END{for(i in a)print i,a[i]}' your_file

Upvotes: 1

fedorqui
fedorqui

Reputation: 289495

This needs formatting:

$ awk 'NR>1 {a[$1$2, $NF]+=$8} END {for (i in a) print i " ==> " a[i]}' file
YAttribute0457 ==> 15
YAttribute0456 ==> 36
YAttribute0012 ==> 5
XAttribute0012 ==> 4
XAttribute1210 ==> 12
XAttribute0154 ==> 1
XAttribute0115 ==> 6
YAttribute1138 ==> 4
XAttribute1138 ==> 66

Explanation

  • NR>1 start processing from second line.
  • {a[$1$2, $NF]+=$8} store values in an array a[X Attribute, Time] with the result of TOT.
  • END {for (i in a) print i " ==> " a[i]} loop through the values of the array and print the output.

Upvotes: 1

Amit Kumar
Amit Kumar

Reputation: 313

awk '{print $1,$2,$10,$8}' file

Upvotes: -1

Related Questions