subodh1989
subodh1989

Reputation: 716

grep from a log file to get count

I have to get certain count from files. The grep statement i am using is like this :

counter_pstn=0
completed_count_pstn=0
rec=0
for rec in `(grep "merged" update_completed*.log | awk '{print $1}' | sed 's/ //g' | cut -d':' -f2)`
do
if [ $counter_pstn -eq 0 ]
then
completed_count_pstn=$rec
else
completed_count_pstn=$(($completed_count_pstn+$rec))
fi
counter_pstn=$(($counter_pstn+1))
done
echo "Completed Orders PSTN Primary " $completed_count_pstn

But the log file contains data in this format :

2500 rows merged.
2500 rows merged.
2500 rows merged.
2500 rows merged.2500 rows merged.
2500 rows merged.
2500 rows merged.

As a result , it is missing out the count of one merge(eg on line 4 of output).How do i modify the grep or use another function to get the count. NOTE that the 2500 number maybe for different logs. So we have to use "rows merged" pattern to get the count. i have tried -o ,-w grep options,but it is not working.

Expected output from above data:

17500

Actual output showing :

15000

Upvotes: 0

Views: 775

Answers (3)

Ed Morton
Ed Morton

Reputation: 204731

Just use awk with '.' as the record separator since that seems to be what you want to indicat the end of each record:

$ cat file
2500 rows merged.
2500 rows merged.2500 rows merged.
2500 rows merged.

$ awk -v RS='.' '/merged/{sum+=$1} END{print "sum=" sum}' file
sum=10000

Upvotes: 2

Vijay
Vijay

Reputation: 67319

perl -p -lne 's/merged./merged.\n/g' your_file|awk '{a+=$1}END{print a}'

Upvotes: 2

Kent
Kent

Reputation: 195289

what is your grep -V output?

anyway, you can do it without grep:

sed 's/merged\./\n/g' file|awk '{x+=$1}END{print x}'

test

kent$  echo "2500 rows merged.
2500 rows merged.
2500 rows merged.
2500 rows merged.2500 rows merged.
2500 rows merged.
2500 rows merged."|sed 's/merged\./\n/g'|awk '{x+=$1}END{print x}'                                                                                                         
17500

Upvotes: 0

Related Questions