Search code examples
grep

Grep all instances from a file and add a specific position from all elements


I have a file that gives me the following when I use grep

$ grep "df vj and vk" output.log
    CPU time for df vj and vk      0.79 sec, wall time      0.10 sec
    CPU time for df vj and vk      0.24 sec, wall time      0.03 sec
    CPU time for df vj and vk      0.27 sec, wall time      0.03 sec
    CPU time for df vj and vk      0.26 sec, wall time      0.03 sec

How can I add the wall time number? The desired output should be 0.19


Solution

  • You could try the following :

    awk -v Total=0 -- '/df vj and vk/{Total += $(NF - 1)}; END { print Total }' output.log
    

    Hope that helps.