3

I am trying to find the number an even occured in my log file.

Command:

grep -Eo "2016-08-30" applciationLog.log* -c 

Output:

 applciationLog.log.1:0
 applciationLog.log.2:0
 applciationLog.log.3:0
 applciationLog.log.4:0
 applciationLog.log.5:7684
 applciationLog.log.6:9142
 applciationLog.log.7:8699
 applciationLog.log.8:0

What I actually need is sum of all these values 7684 + 9142 + 8699 = 25525. Any suggestion I can do it? Anything I can append to the grep to enable it.

Any help or pointers are welcome and appreciated.

Marc B
  • 356,200
  • 43
  • 426
  • 500
user2866507
  • 166
  • 2
  • 5
  • grep's job is to find things. adding up numbers is not its job. you feed grep's output to awk/sed and do the addition there. – Marc B Sep 01 '16 at 17:29

7 Answers7

2

If you want to keep your grep command, pipe its output to awk, the quick and dirty way is down here:

grep -Eo "aaa" -c aaa.txt bbb.txt -c | awk 'BEGIN {cnt=0;FS=":"}; {cnt+=$2;}; END {print cnt;}'

Or use use awk regex directly:

awk 'BEGIN {cnt=0}; {if(/aaa/) {cnt+=1;}}; END {print cnt;}' aaa.txt bbb.txt
alagner
  • 3,448
  • 1
  • 13
  • 25
2

As addition to the already given answer by ghoti:

You can avoid awk -F: by using grep -h:

grep -c -h -F "2016-08-30" applicationLog.log* | awk '{n+=$0} END {print n}'

This means no filenames and only the counts are printed by grep and we can use the first field for the addition in awk.

0

See if this works for you:

grep -Eo "2016-08-30" applciationLog.log* -c | awk -F':' 'BEGIN {sum = 0;} {sum += $2;} END {print sum;}'

We use awk to split each line up with a delimeter of :, sum up the numbers for each line, and print the result at the end.

John
  • 2,395
  • 15
  • 21
  • 1
    Nice answer! ;-) FYI, awk doesn't require semicolons *at the end of each command*, it only requires them for separation if multiple lines within the same statement are on the same line. So all the semicolons in your awk script can be dropped. – ghoti Sep 01 '16 at 17:42
  • @ghoti Understood, made it as plain and verbose as possible to make it potentially easier to follow for someone unfamiliar with awk – John Sep 01 '16 at 17:43
0

The grep command doesn't do arithmetic, it just finds lines that match regular expressions.

To count the output you already have, I'd use awk.

grep -c -F "2016-08-30" applciationLog.log* | awk -F: '{n+=$2} END {print n}'

Note that your grep options didn't make sense -- -E tells the command to use Extended regular expressions, but you're just looking for a fixed string (the date). So I swapped in the -F option instead. And -o tells grep to print the matched text, which you've overridden with -c, so I dropped it.

ghoti
  • 45,319
  • 8
  • 65
  • 104
0

An alternative using for-loop and arithmetic expansion could be:

x=0
for i in $(grep -hc "2016-08-30" applciationLog.log*);do
    x=$((x+i))
done
echo "$x"
0

An easy alternative is to merge all the files before grep sees them:

cat applciationLog.log* | grep -Eo "2016-08-30" -c 
Thomas
  • 174,939
  • 50
  • 355
  • 478
0

In my directory have have hundreds of files, each file contains lot of text along with a lines similar to this-

Job_1-Run.log:[08/27/20 01:28:40] Total Jobs Cancelled for Job_1_set0 = 10

I do grep '^Total Jobs Cancelled' ./* to get that above line.

Then I do a pipe | awk 'BEGIN {cnt=0;FS="="}; {cnt+=$2;}; END {print cnt;}'

so my final command is-

grep '^Total Jobs Cancelled' ./* | awk 'BEGIN {cnt=0;FS="="}; {cnt+=$2;};END {print cnt;}'

and result is the sum. e.g. - 900

I am using Cmder @ https://cmder.net/

Thanks to the answer by @alagner, @john above

pa_vishal
  • 21
  • 4