having problem with for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting having problem with for loop
# 1  
Old 10-16-2008
having problem with for loop

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
# 2  
Old 10-16-2008
Try this...

Code:
grep "2008-10-14"  Archiver* | grep 'CleanLPDataMessage: Missing Intervals' | awk '$6=="CleanLPDataMessage:" {t+=$9} {next} END {print "Total CleanLPDataMessage: Missing Intervals is "t}'

Code:
grep "2008-10-14"  Archiver* | grep 'CleanLPDataMessage: Missing Intervals' | awk '{t+=$9} {next} END {print "Total CleanLPDataMessage: Missing Intervals is "t}'


Last edited by manosubsulo; 10-16-2008 at 06:49 AM..
# 3  
Old 10-16-2008
Here's the portion from your while loop:

Code:
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:
Code:
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.
MrC
# 4  
Old 10-16-2008
hie

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
# 5  
Old 10-16-2008
Quote:
Originally Posted by er_aparna
I think for loop doesn't accepts complete cat command inside it ,
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.
MrC
# 6  
Old 12-09-2008
Question

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
# 7  
Old 12-09-2008
Basically add 3 number and show the results
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with loop

Hi all, a problem with a loop. Imagine it starts with var1="$(cat txtif.out )"while do echo file1 echo file2 echo y > txtif.out if then break fi done exit 0 the problem is that if the file is changing during the loop seems to continue as it reads "x" state,... (5 Replies)
Discussion started by: Board27
5 Replies

2. Shell Programming and Scripting

Problem with While Do loop

I am trying to do a while do loop that asks for a password and returns if the password entered is incorrect. I have that part working however i want to modify it so that if a similar word is entered it will read "that is close but incorrect" and i cannot seem to get it working, being new in the... (6 Replies)
Discussion started by: lm5522
6 Replies

3. Shell Programming and Scripting

Problem with loop within loop

Hi, I work on Ab-initio ETL tool which is based on Unix. I made a small script which has two loop's one with in another. All the functionality is working for the first line of outer loop but when it comes to other lines of outer loop it is throwing error as command not found. Below is the... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

4. Shell Programming and Scripting

Problem with While loop.

Hi Script Gurus, I am facing issue with while loop in bash. The while loop is running as end less loop even after given criteria does not meet. STATE_BEFORE_SPLIT () { for clone in $Clonegroups; do state=$( $Navicmd -listclone -name $clone -cloneid $Cloneid |awk... (3 Replies)
Discussion started by: RobP
3 Replies

5. UNIX for Dummies Questions & Answers

While loop problem

I have a while loop with -f and -o option.Can any one please tell me what that stands for?The Sample code is as follows:- while do ## some processing done (10 Replies)
Discussion started by: Param0073
10 Replies

6. Shell Programming and Scripting

problem with while loop

hi I had created a while loop in a script file called whiletest.sh as follows as follows: count=0 max=10 while do echo $count echo count=$(count+1)" " done echo "value of count:$count" then i run the script with the command sh whilestest.sh but its giving me an error... (5 Replies)
Discussion started by: angel12345
5 Replies

7. Shell Programming and Scripting

Problem in While loop

Hi Guys, This is my code.. I am getting an error in the inside while statement when comparing.. I am not able to figure it out.. Pls help me... while read value fatherid son_id top_id r_type do inside_value=1; echo $inside_value; echo $r_type; while do echo... (6 Replies)
Discussion started by: mac4rfree
6 Replies

8. UNIX for Advanced & Expert Users

loop problem

Hi guys my while do loop is not working properly; As soon as the load_date and run_date is same it should stop can somebody tell me where I am having the problem? In an oracle table I have LOAD_DT=5/1/2009 DATE datatype RUN_DT=5/5/2009 DATE datatype Now in a script with a spool file I get the... (15 Replies)
Discussion started by: henrysmith
15 Replies

9. Shell Programming and Scripting

for loop problem

Hi, I have a directory called logs in which i have the log files. i have to touch the file before deleting it. i am doing like this filestodelete="*.log* *log*" for files in $filestodelete do touch $files $files.$(date +%a) rm -f $filestodelete done touch is not working... (5 Replies)
Discussion started by: namishtiwari
5 Replies

10. Programming

problem with while loop

hi all, i have written the following code: while(proceed !='Y' && proceed!='N' && proceed!='y' && proceed!='n') { printf("\nPress \n\t 'Y' or 'y' to continue \n\t 'N' or 'n' to cancel:"); scanf("%c",&proceed); } the output i am gettin is: Press 'Y' to continue ... (1 Reply)
Discussion started by: mridula
1 Replies
Login or Register to Ask a Question