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?

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

When you store a line of input into a variable you must echo it thru pipe:

q1=`echo $j | grep 'INFO - LPBatch:' | wc -l`

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?

Have you tried running your script with debugging? In sh it's -x

sh -x script.sh

Or add

set -x

at the begining of the script.

just try awk '{print \$1}'

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

I put in the missing echos to your script and had the $q changed to "${q}" just to make sure the value/strings will be used completely and I got some output in nav1.txt. Here is your changed script.

If you like you can explain what you need from this line so we can have it a bit easier and compact with maybe 1-2 lines of code. Nevertheless, here you go:

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

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

Output of nav1.txt:

1-GH32X, CC, AMR,
Number of Intervals Not Inserted: 1 / 95
1-VIN74, CC, AMR,

Not sure if that is the stuff you want though.

When I modified your script from the first post:

#!/usr/bin/sh

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

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

Got this output:

$ cat nav1.txt 
Number of Intervals Not Inserted: 1 / 95
1-GH32X, CC, AMR,
Number of Intervals Not Inserted: 1 / 95
1-VIN74, CC, AMR,

With no errors.

ok. thanks everybody for their precious help. i got the o/p with echo "${J}"

also i have little modified the script to add elif function. can any1 tell
where i m going wrong below in the elif part

while read j
do
q1=0
q2=0

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

q2=`echo "${j}" | grep 'INFO - Number of Intervals Not Inserted:' | wc -l`
elif [ $q2 -eq 1 ]
then
echo "${j}" | tr -s " " | cut -d " " -f6-13 >> nav1.txt
fi

else
echo "${j}" >> asma
fi

done < nav11.txt

You finish the if sentence with fi and then start an elif ... ?
It should go like this:
if [ something ]; then
something
elif [ somethingelse ]; then
somethingelse
else
whenallelsefails
fi

Please use [ code ] and [ /code ] to display your code in those fancy eye friendly blue boxes. Also please describe what is not working now since you last changes.

To shorten it up, maybe you just post what's the wanted output.

my mistake a silly one .Actually if elif should be ended with one fi.

I really appreciate everbody's help. hatz off to all unix guru