Skip to main content
Edit for readability
Source Link
Rastapopoulos
  • 1.9k
  • 2
  • 13
  • 21

For the first part, I think something like this would work:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '
    { 
        for (i = 1; i <= NF; i += 2) {
            a[$i]+=$(i+1); 
        }
    } 
    END {
        for (i in a) print i, "=", a[i]
    }'

This code produces the following output:

PASSIVE__SERVICE__CHECK_ = 288
Warning___Return = 278
Auto_save__of__retention__data__completed = 6
SERVICE__ALERT_ = 306
Warning___The__results__of__service = 96
on__host = 386
Warning___on__host = 463

The only potential problem here is that the order is not the same as the one of the original file. If the order matters to you, you can use:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '
    { 
        for (i = 1; i <= NF; i += 2) {
            a[$i]+=$(i+1); b[i]=$i
            b[i]=$i;
        }
    } 
    END {
        for (i in b) print b[i], "=", a[b[i]]
    }'

but it's not as easy to read.

For your second question, I think that this works to calculate the sum, although it might not be the shortest or most efficient way to do it.

cat data.now | awk -F, '{print $9}' | 
    sed "s/.*\<${Pattern}=\([0-9]\+\).*/\1/g" | 
    awk '{s += $1} END {print s}'

For the first part, I think something like this would work:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '{ for (i = 1; i <= NF; i += 2) a[$i]+=$(i+1)} END {for (i in a) print i, "=", a[i]}'

This code produces the following output:

PASSIVE__SERVICE__CHECK_ = 288
Warning___Return = 278
Auto_save__of__retention__data__completed = 6
SERVICE__ALERT_ = 306
Warning___The__results__of__service = 96
on__host = 386
Warning___on__host = 463

The only potential problem here is that the order is not the same as the one of the original file. If the order matters to you, you can use:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '{ for (i = 1; i <= NF; i += 2) {a[$i]+=$(i+1); b[i]=$i}} END {for (i in b) print b[i], "=", a[b[i]]}'

but it's not as easy to read.

For your second question, I think that this works to calculate the sum, although it might not be the shortest or most efficient way to do it.

cat data.now | awk -F, '{print $9}' | sed "s/.*\<${Pattern}=\([0-9]\+\).*/\1/g" | awk '{s += $1} END {print s}'

For the first part, I think something like this would work:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '
    { 
        for (i = 1; i <= NF; i += 2) {
            a[$i]+=$(i+1); 
        }
    } 
    END {
        for (i in a) print i, "=", a[i]
    }'

This code produces the following output:

PASSIVE__SERVICE__CHECK_ = 288
Warning___Return = 278
Auto_save__of__retention__data__completed = 6
SERVICE__ALERT_ = 306
Warning___The__results__of__service = 96
on__host = 386
Warning___on__host = 463

The only potential problem here is that the order is not the same as the one of the original file. If the order matters to you, you can use:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '
    { 
        for (i = 1; i <= NF; i += 2) {
            a[$i]+=$(i+1); 
            b[i]=$i;
        }
    } 
    END {
        for (i in b) print b[i], "=", a[b[i]]
    }'

but it's not as easy to read.

For your second question, I think that this works to calculate the sum, although it might not be the shortest or most efficient way to do it.

cat data.now | awk -F, '{print $9}' | 
    sed "s/.*\<${Pattern}=\([0-9]\+\).*/\1/g" | 
    awk '{s += $1} END {print s}'
Source Link
Rastapopoulos
  • 1.9k
  • 2
  • 13
  • 21

For the first part, I think something like this would work:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '{ for (i = 1; i <= NF; i += 2) a[$i]+=$(i+1)} END {for (i in a) print i, "=", a[i]}'

This code produces the following output:

PASSIVE__SERVICE__CHECK_ = 288
Warning___Return = 278
Auto_save__of__retention__data__completed = 6
SERVICE__ALERT_ = 306
Warning___The__results__of__service = 96
on__host = 386
Warning___on__host = 463

The only potential problem here is that the order is not the same as the one of the original file. If the order matters to you, you can use:

cat data.now | awk -F, '{print $9}' | sed 's/=/ /g' | awk '{ for (i = 1; i <= NF; i += 2) {a[$i]+=$(i+1); b[i]=$i}} END {for (i in b) print b[i], "=", a[b[i]]}'

but it's not as easy to read.

For your second question, I think that this works to calculate the sum, although it might not be the shortest or most efficient way to do it.

cat data.now | awk -F, '{print $9}' | sed "s/.*\<${Pattern}=\([0-9]\+\).*/\1/g" | awk '{s += $1} END {print s}'