How can I add 4 lines together and then read the next 4 lines?

# awk 'END {
> if(t)
> print e, "Total:",t
> }
> /\.exe/ {
> if (t)
> print e, "Total:", t
> t = 0
> e = $0
> }
> /Total/ { t +=$NF }
> ' nisse_081121_11.log
#

No output

Only Total, or Total and exe?

It should return the lines 081125_08:38 14315 2.39 SOM.exe ... too.

awk '/Total|\.exe/' nisse_081121_11.log

081125_09:38 14315 2.32 SOM.exe
AO Total = 9002
AO Total = 2543
AO Total = 434
AO Total = 45241

I got both..

Try changing the code like this:

awk -F= 'END {
  if (t) 
    print e, "Total:", t
}
/\.exe/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' nisse_081121_11.log

Thanks for your effort now it works.

# awk 'END {
> if (t)
> print e, "Total:", t
> }

if (t)
> /\.exe/ {
> if (t)
> print e, "Total:", t
> t = 0
> e = $0 }
/Total/ { t += $NF }
' nisse_081121_11.log
> }
> /Total/ { t += $NF }
> ' nisse_081121_11.log
081125_08:38 14315 2.39 798928 198257 SOM.exe Total: 56098
081125_09:38 14315 2.32 798928 198257 SOM.exe Total: 57220
#

If I want to search for another word, would it just do to change Total?

/Total/ { t += $NF }

And if it is not that clean as it is now with the numbers. If it was text after it. I.e

Total houses = 32245 houses

Would it still work?

It should, try it.
If it looks like a number, for AWK it is a number.

It did not work.

I added
AO Total = 3423 Alarm Objects

Does awk interpret it as the same as without "Alarm Objects"?

I cannot reproduce it, could you post sample data, commands and output:

$ print AO Total = 3423 Alarm Objects | awk -F= '{print $NF+1}'
3424

I am sorry. I mixed it up I guess..

It works with

awk -F= 'END {
  if (t)
    print e, "Total:", t
}
/\.exe/ {
  if (t)
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' nisse_081121_11.log

But I do not get it working if I add the code to a script.

Please post your script and try to explain what exactly is the problem with it.

cat test
END {
if (t)
print e, "Total:", t
}
/\.exe/ {
if (t)
print e, "Total:", t
t = 0
e = $0
}
/Total/ { t += $NF }

#awk -f test nisse_081121_11.log
081125_08:38 14315 2.39 SOM.exe Total: 56098
081125_09:38 14315 2.32 SOM.exe Total: 57220

It works when the it looks lite A0 Total = 12333, but not when I have
A0 Total = 12333 Alarm Objects

#cat nisse_081121_11.log

DATE PID CPU COMMAND
081125_08:38 14315 2.39 SOM.exe

            OPERATIONAL CONTEXT 1
                           AO Total = 8867 Alarm Objects
                     AO Outstanding = 42
                    AO Acknowledged = 0
                      AO Terminated = 8825
                     AO Not Handled = 42
                         AO Handled = 0
                          AO Closed = 8825
                        AO Archived = 0

      OPERATIONAL CONTEXT 2
                           AO Total = 2570 Alarm Objects
                     AO Outstanding = 2365
                    AO Acknowledged = 32
                      AO Terminated = 173
                     AO Not Handled = 2397
                         AO Handled = 0
                          AO Closed = 173
                        AO Archived = 0

      OPERATIONAL CONTEXT 3
                           AO Total = 403 Alarm Objects
                     AO Outstanding = 312
                    AO Acknowledged = 2
                      AO Terminated = 89
                     AO Not Handled = 314
                         AO Handled = 0
                          AO Closed = 89
                        AO Archived = 0

      OPERATIONAL CONTEXT 4
                           AO Total = 44258 Alarm Objects
                     AO Outstanding = 650
                    AO Acknowledged = 0
                      AO Terminated = 43608
                     AO Not Handled = 650
                         AO Handled = 0
                          AO Closed = 43608
                        AO Archived = 0

DATE PID CPU VSZ SZ COMMAND
081125_09:38 14315 2.32 798928 198257 SOM.exe

            OPERATIONAL CONTEXT 1
                           AO Total = 9002
                     AO Outstanding = 46
                    AO Acknowledged = 0
                      AO Terminated = 8956
                     AO Not Handled = 46
                         AO Handled = 0
                          AO Closed = 8956
                        AO Archived = 0

      OPERATIONAL CONTEXT 2
                           AO Total = 2543
                     AO Outstanding = 2357
                    AO Acknowledged = 32
                      AO Terminated = 154
                     AO Not Handled = 2389
                         AO Handled = 0
                          AO Closed = 154
                        AO Archived = 0

      OPERATIONAL CONTEXT 3
                           AO Total = 434
                     AO Outstanding = 312
                    AO Acknowledged = 2
                      AO Terminated = 120
                     AO Not Handled = 314
                         AO Handled = 0
                          AO Closed = 120
                        AO Archived = 0

      OPERATIONAL CONTEXT 4
                           AO Total = 45241
                     AO Outstanding = 664
                    AO Acknowledged = 0
                      AO Terminated = 44577
                     AO Not Handled = 664
                         AO Handled = 0
                          AO Closed = 44577
                        AO Archived = 0

> awk -f test nisse_081121_11.log
081125_09:38 14315 2.32 SOM.exe Total: 57220
[root@knasen]>

You should run the script like this:

awk -F= -f test nisse_081121_11.log

Or, if you prefer, change the AWK code like this:

END {
  if (t)
    print e, "Total:", t
  }
/\.exe/ {
  if (t)
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
BEGIN { FS = "=" }

... and run the script like you did before.

Thanks a lot for your perseverance. I appreciate it a lot. It helped me to rationalize the work. :slight_smile: