1

I have a command I am using

#cat test |grep "Ongoing Calls" |awk -F' ' '{print $3}'
40
2

As you can see it returns 2 values 40 & 2

I want to now add these together and put into a file as 42

any ideas

edit here is what is in the test file - all I want is the values in OnGoing calls and sumed together

    Iedge Group                                               1
    RasIpAddr                                                 0.0.0.0/0
    SubnetIP
    SubnetMask
    Max Calls                                                 50 (0:-1)
    Max 911 Overflow Calls                                    0 (0:0)
    Ongoing Calls                                             2 (0:2)
    Ongoing Calls                                             40 (40:0)
    Max Src Hunting                                           0
    Max number of simultaneously allowed transfer calls       0
    Ongoing transfer calls                                    0
    Priority                                                  0
    Call Duration                                             0
    Idle TCP/TLS/SCTP Connection Timeout                               0
    Bi-directional TCP/SCTP connections                            enabled
    Capabilities                                              gateway
    SIP                                                       enabled
    H323                                                      disabled
    Zone

when I used this command

    awk '/Ongoing Calls/ { sum += 3 } END { print sum }' test `

I keep getting the value 3 if there is nothing in the OnGoing calls section

  • you can do it all in awk. Also show what is in test. Also format your question. – 123 Jul 7 '16 at 10:20
  • ok, formatting updated... (sorry first time using stackexchange) - can you give me advice on how to do it all in awk? thanks – WMK83 Jul 7 '16 at 10:27
  • show what is in test – 123 Jul 7 '16 at 10:28
  • the file is long so ill just pull the relevant info out – WMK83 Jul 7 '16 at 10:31
  • Please post it in the question formatted. Yes a sample is all that is needed. – 123 Jul 7 '16 at 10:32
2

Change your awk script to

awk '{ sum += $3 } END { print sum }'

As user @123 points out, the whole pipeline may be replaced with a single call to awk:

awk '/Ongoing Calls/ { sum += $3 } END { print sum }' test
  • 1
    missing /Ongoing Calls/ – 123 Jul 7 '16 at 10:31
  • this worked, I just tested it – WMK83 Jul 7 '16 at 10:34
  • @123 if you want to replace the call to grep too, yes. If not, then it's not needed. – Kusalananda Jul 7 '16 at 10:50
  • Why would you not want to replace the call to grep... – 123 Jul 7 '16 at 12:04
  • @123 I didn't say I didn't want to, I was only answering the OP's question about how to sum the values that grep produced. – Kusalananda Jul 7 '16 at 12:05
0

I'm not saying this is better than using awk:

sed -rn 's/Ongoing Calls *([0-9]+).*/\1/p' test | paste -sd+ | bc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.