problem in while loop in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in while loop in a script
# 1  
Old 10-16-2008
problem in while loop in a script

i have a script that will read each line and then grep a particular pattern and do some_stuff. Below the script

while read j
do
q1=0
q1=`$j | grep 'INFO - LPBatch:' | wc -l`
if [ $q1 -eq 1 ]
then
$j | tr -s " " | cut -d " " -f8,42,43 >> nav1.txt
fi

q2=0
q2=`$j | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`
if [ $q2 -eq 1 ]
then
$j | tr -s " " | cut -d " " -f6-13 >> nav1.txt
fi
done < nav11.txt
-----------------------------------------------------------------------

below some of the lines of the file nav11.txt


2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1

2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]

2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95

2008-10-14 05:02:56,762 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 3

2008-10-14 05:02:56,763 [Thread-6] INFO - LPBatch: [null, 1-VIN74, null, DC:Tue Oct 14 09:29:49 UTC 2008, null, null, Mon Oct 13 04:00:00 CDT 2008, Mon Oct 13 09:00:00 UTC 2008, Tue Oct 14 04:00:00 CDT 2008, Tue Oct 14 09:00:00 UTC 2008, CC, AMR, 97]

2008-10-14 05:02:56,763 [Thread-7] INFO - Register Read: [1-X22K8, 0.0, Tue
------------------------------------------------------------------------

the while part is having some problem.can anyone help me in this?

Last edited by ali560045; 10-16-2008 at 08:05 AM..
# 2  
Old 10-16-2008
awk '/LPBatch/{print $0}' test1|cut -d" " -f6-13

Try the above. In my test I got the out put like

LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37
LPBatch: [null, 1-VIN74, null, DC:Tue Oct 14 09:29:49
# 3  
Old 10-16-2008
When you store a line of input into a variable you must echo it thru pipe:
Code:
q1=`echo $j | grep 'INFO - LPBatch:' | wc -l`

# 4  
Old 10-16-2008
ok. got that but still nothing is comming to o/p file nav1.txt

the while is reading file nav11.txt line by line and then grepping for either 'INFO - LPBatch:' or 'INFO - Number of Intervals Not Inserted:' and if either of them found then it is not going to their respective if condition ?


is anything wrong with the if condition in the while loop?

Last edited by ali560045; 10-16-2008 at 07:54 AM..
# 5  
Old 10-16-2008
Have you tried running your script with debugging? In sh it's -x
Code:
sh -x script.sh

Or add

Code:
set -x

at the begining of the script.
# 6  
Old 10-16-2008
problem with while loop

just try awk '{print \$1}'
# 7  
Old 10-16-2008
yes i did put set -x and got that while is indeed reading line by line but if found grepped pattern not going to their respective if condition. below the debugging o/p:


+ test -f nav1.txt
+ 0< asma
+ read j
+ q1=0
+ q2=0
+ + grep INFO - LPBatch:
+ echo 2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
./w.sh: Resource temporarily unavailable
q1= 0
+ [ 0 -eq 1 ]
+ + grep INFO - Number of Intervals Not Inserted:
+ echo 2008-10-14 05:47:05,551 [Thread-6] ERROR - CleanLPDataMessage: Missing Intervals: 1
+ wc -l
q2= 0
+ [ 0 -eq 1 ]
+ read j
+ q1=0
+ q2=0
+ + wc -l
+ grep INFO - LPBatch:
+ echo 2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]
q1= 0
+ [ 0 -eq 1 ]
+ + echo 2008-10-14 05:47:05,551 [Thread-6] INFO - LPBatch: [null, 1-GH32X, null, DC:Tue Oct 14 10:12:37 UTC 2008, null, null, Mon Oct 13 05:00:00 CDT 2008, Mon Oct 13 10:00:00 UTC 2008, Tue Oct 14 04:45:00 CDT 2008, Tue Oct 14 09:45:00 UTC 2008, CC, AMR, 94]
+ wc -l
+ grep INFO - Number of Intervals Not Inserted:
q2= 0
+ [ 0 -eq 1 ]
+ read j
+ q1=0
+ q2=0
+ + wc -l
+ grep INFO - LPBatch:
+ echo 2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95
q1= 0
+ [ 0 -eq 1 ]
+ + echo 2008-10-14 05:47:05,575 [Thread-6] INFO - Number of Intervals Not Inserted: 1 / 95
+ grep INFO - Number of Intervals Not Inserted:
+ wc -l
q2= 0
+ [ 0 -eq 1 ]
+ read j
+ echo completed
completed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL script loop problem

I have written the below PERL script to reprocess messages from a failure queue. It basically browses all the messages in the failure queue to individual files in a directory and then scans those files to determine the originating queue. The script will then move each message in turn from the... (0 Replies)
Discussion started by: chris01010
0 Replies

2. 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

3. Linux

Problem with my loop and awk script

Sorry if this is a super simple issue, but am extremely new to this and am trying to teach myself as I go along. But can someone please help me out? I have a data file similar to this for many samples, for all chromosomes Sample Chr bp p roh Sample1 1 49598178 0 1... (14 Replies)
Discussion started by: vuvuzelo
14 Replies

4. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

5. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

6. Shell Programming and Scripting

How to loop use while loop in csh script?

Hi all, i got 2 text file. file.txt value.txt i want use C shell script to write out while both of the file got different limit....how i going to write it in 1 while loop? (4 Replies)
Discussion started by: proghack
4 Replies

7. Shell Programming and Scripting

while loop problem in c shell script

Hi all, i write a script c shell set i = 1 while ( $i <= $#array ) echo "$array" @ i++ end i want to set it to i = i +2 in that statement . Can anybody help me? ---------- Post updated at 02:46 PM ---------- Previous update was at 02:35 PM ---------- anybody not how to solve it??? (2 Replies)
Discussion started by: proghack
2 Replies

8. Shell Programming and Scripting

Problem with while loop in shell script

Hi All, How to read a file upto last line(End Of Line) I wrote below program: cat R2_20060719.610.txt | while read LINE do echo "$LINE" done above code reading all lines from a file and skipping last line...... is there anything wrong in my code. Please help me out from this... (20 Replies)
Discussion started by: rkrgarlapati
20 Replies

9. UNIX for Dummies Questions & Answers

have a problem with if elif loop .. plz help me with the script

count2=0 var2=NOT if then echo"Loop1" command="egrep ',$var1," if then echo "the command is OR" command=$command"|,$var3," echo "$command" elif then command=$command"| egrep ',$var3," else ... (4 Replies)
Discussion started by: Syms
4 Replies

10. Shell Programming and Scripting

Shell Script loop problem

I am writing a shell script that simulates the `wc -w` command without actually using wc itself. My problem is that the script will only read the first line of the file and just keep looping through it. I have tried both while and for loops and got the same result. Can anyone help? ... (1 Reply)
Discussion started by: MaxMouse
1 Replies
Login or Register to Ask a Question