i have the below script that will grep a particular pattern and then will compute the sum of its value.
TZ=`date +%Z`+48 ;a=`date +%Y-%m-%d`
echo $a
sum=0
num=0
for i in `cat Archiver* | grep "$a" | grep 'ERROR' | grep 'CleanLPDataMessage: Missing Intervals'`
do
num=`$i | tr -s " " | cut -d " " -f9`
sum=`expr $sum + $num`
done
echo "Total CleanLPDataMessage: Missing Intervals is $sum"
-------------------------------------------------------------------------
if i use the below command it's work fine
cat Archiver* | grep "2008-10-14" | grep 'CleanLPDataMessage: Missing Intervals' | head -5
2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
2008-10-14 05:50:54,072 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
2008-10-14 05:55:41,604 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
2008-10-14 05:59:56,391 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
2008-10-14 06:00:04,117 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 3
--------------------------------------------------------------------------
how to use the above command in for loop so that my sum should be 1+1+1+1+3=7
Try this...
grep "2008-10-14" Archiver* | grep 'CleanLPDataMessage: Missing Intervals' | awk '$6=="CleanLPDataMessage:" {t+=$9} {next} END {print "Total CleanLPDataMessage: Missing Intervals is "t}'
grep "2008-10-14" Archiver* | grep 'CleanLPDataMessage: Missing Intervals' | awk '{t+=$9} {next} END {print "Total CleanLPDataMessage: Missing Intervals is "t}'
system
October 16, 2008, 5:49am
3
Here's the portion from your while loop:
while read line
do
num=$(echo $line | tr -s " " | cut -d " " -f9)
sum=`expr $sum + $num`
done < <( grep -h $a Archiver* | grep 'ERROR' | grep 'CleanLPDataMessage: Missing Intervals' )
echo "Total CleanLPDataMessage: Missing Intervals is $sum"
But the entire while loop and its process substitution can be done more simply in awk or perl:
perl -ne 'END {print "Total CleanLPDataMessage: Missing Intervals is $sum\n"} /^'"$a"'.* ERROR - CleanLPDataMessage: Missing Intervals: (\d+)$/ and $sum += $1' Archiver*
All you need to do is to set a, your data variable just as you did above.
I think for loop doesn't accepts complete cat command inside it ,
You can first have output in another file and the use that file in for loop like
TZ=`date +%Z`+48 ;a=`date +%Y-%m-%d`
echo $a
grep "$a" Archiver* | grep 'ERROR' | grep 'CleanLPDataMessage: Missing Intervals' > temp1
sum=0
num=0
for i in `cat temp1`
do
num=`$i | tr -s " " | cut -d " " -f9`
sum=`expr $sum + $num`
done
echo "Total CleanLPDataMessage: Missing Intervals is $sum"
------------------------------------------------------------------------
try this
Tc- Aparna
system
October 16, 2008, 5:57am
5
Sure it will. The OPs solution caused the output of the Archives files to be sent through a series of pipes. The problem was that then that output was word split, so the for loop was iterating over words, not lines.
twan
December 9, 2008, 5:18pm
6
does anyone know how to write a script file that sums the numbers passed to it as arguments on the command line and display the results?
thanks
twan
twan
December 9, 2008, 5:47pm
7
Basically add 3 number and show the results
Dear twan don't ask new question inside a thread
if you wanna ask please start a new thread.
simply write this inside the script
echo $((sum=$1+$2+$3))
system
December 9, 2008, 6:12pm
9
$ cat ~/bin/sum
#!/bin/bash
echo $@ | sed 's/ */+/g' | bc
$ ~/bin/sum 1 5 10
16
$ ~/bin/sum 2
2
$ ~/bin/sum 2 4 6 8
20
$ ~/bin/sum 1 2 3 4 5 6 7 8 9 10
55