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

Hi,

I have grep:ed on a file with which gives the below output.
Total = 43
Total = 21
Total = 23
Total = 2
Total = 3
Total = 1
Total = 0

I would like to add the first 4th lines to a total amount, and do so on for the rest of the lines.

I was thinking in terms of using while read. But it might perhaps not be possible?

grep "Total" file while read kr
do
add the first 4 th lines. Add them to a variable total
Echo "Total $total"
done

Use nawk or /usr/xpg4/bin/awk on Solaris:

$ print 'Total = 43
Total = 21
Total = 23
Total = 2
Total = 3
Total = 1
Total = 0'|awk '
END{print s}{s+=$NF}
!(NR%4){print s;s=0}
'                   
89
4

Or:

$ print 'Total = 43
Total = 21
Total = 23
Total = 2
Total = 3
Total = 1
Total = 0'|awk '
END{print s}{s+=$NF}1
!(NR%4){print s;s=0}
'
Total = 43
Total = 21
Total = 23
Total = 2
89
Total = 3
Total = 1
Total = 0
4

Thanks.

If I have a variable containing the same number of lines as the output would give me.

How can I add those two together? I.e

2/22 Total 89
3/11 Total 4
etc...

if I store it in a variable total=
total = grep Total file |awk '
END{print s}{s+=$NF}
!(NR%4){print s;s=0}
'
and I have the
nr=grep dtates file

I want to somehow combine the first row with the first row and
etc.

Please post sample input data (no grep etc.) and an example of the desired output based on that input.

081121_13:43 8606 0.87 SOM.exe
081121_14:43 8606 1.85 SOM.exe
081121_15:43 8606 1.10 SOM.exe

Total = 268
Total = 9392
Total = 2625
Total = 34
Total = 3434
Total = 5667
Total = 322
Total = 333
Total = 1231
Total = 2
Total = 99
Total = 11

I Would like to have the outprint as
081121_13:43 8606 0.87 SOM.exe Total =Sum{268+9392+2625+34}
081121_14:43 8606 1.85 SOM.exe Total =Sum{3434+5667+322+333}
081121_15:43 8606 1.10 SOM.exe Total =Sum{1231+2+99+11}

awk 'END {
  if (s) 
    printf "%s Total=%d\n", _[++i], s
  }
!/Total/ { 
  _[++c] = $0
  next  
  }  
{ s += $NF }
!(++n % 4) { 
  printf "%s Total=%d\n", _[++i], s
  s = 0
  }' infile

Thanks for your reply.

I don�t get it working though.

If I have an output with grep from one file that gives me
081121_13:43 8606 0.87 SOM.exe
081121_14:43 8606 1.85 SOM.exe
081121_15:43 8606 1.10 SOM.exe

And an output from another with grep

Total = 268
Total = 9392
Total = 2625
Total = 34
Total = 3434
Total = 5667
Total = 322
Total = 333
Total = 1231
Total = 2
Total = 99
Total = 11

How will I get it working with your solution? Because I do not entirely understand it.

I was asking for the real input files, not their grepped versions :slight_smile:
Could you post samples from both input files (obfuscating sensitive data, if needed)?

Sorry I misunderstood :slight_smile:

081125_08:38 14315 2.39 SOM.exe

    OPERATIONAL CONTEXT 1
                           AO Total = 8867 
                     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 
                     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 
                     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 
                     AO Outstanding = 650 
                    AO Acknowledged = 0 
                      AO Terminated = 43608 
                     AO Not Handled = 650 
                         AO Handled = 0 
                          AO Closed = 43608 
                        AO Archived = 0 

081125_09:40 14315 2.32 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 

And I want to read the file and make an ouput like
081125_09:38 14315 2.32 SOM.exe Total {sum of the 4 Total}
081125_09:40 14315 2.32 SOM.exe Total {sum of the 4 Total }

Is this a single file?

yes it is

Try this:

awk 'END {
  if (t) 
    print e, "Total:", t
    }
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' infile

I am not entirely with you.
I have put this piece of code in a file and try to execute it. But nothing happens.
./test < infile

Could you execute the script on the command line like this and post the result:

$ cat file
081125_08:38 14315 2.39 SOM.exe

OPERATIONAL CONTEXT 1
AO Total = 8867
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
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
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
AO Outstanding = 650
AO Acknowledged = 0
AO Terminated = 43608
AO Not Handled = 650
AO Handled = 0
AO Closed = 43608
AO Archived = 0


081125_09:40 14315 2.32 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 'END {
  if (t) 
    print e, "Total:", t
}
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' file
081125_08:38 14315 2.39 SOM.exe Total: 56098
081125_09:40 14315 2.32 SOM.exe Total: 57220
$ 

If you want to put it in a script you can do something like this:

  1. Prepare a script with the following content:
END {
  if (t) 
    print e, "Total:", t
  }
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
  1. Run the script on the command line:
awk -f <scriptname> <your_datafile>

# 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
#
Nothing happened..

[root@knasen]> cat test
#!/usr/bin/sh
END {
if (t)
print e, "Total:", t
}
/exe$/ {
if (t)
print e, "Total:", t
t = 0
e = $0
}
/Total/ { t += $NF }
[root@knasen]> awk -f test nisse_081121_11.log
[root@knasen]>

No output..

Am I doing it from the wrong shell?

What platform (OS) is this?

P.S. You should remove the first line - #!/usr/bin/sh - from the file test.

What you get when you type awk<RETURN>?

It is HP-UX Itanium

I didnt have the #!/usr/bin/sh first, but when I didnt see any output I added it.

[root@knasen]> awk
Usage: awk [-F fs][-v Assignment][-f Progfile|Program][Assignment|File] ...

Hm, I've just tested the code on HP-UX machine (HP-UX B.11.11 U 9000/800) and it works fine. Could you run the following command and post the output:

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

That worked fine.. It extracted all ao total..

Ahhhh, I think I understand, try changing the code from:

/exe$/

to

/\.exe/