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
# 8  
Old 10-16-2008
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:

Code:
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:
Code:
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.
# 9  
Old 10-16-2008
When I modified your script from the first post:

Code:
#!/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:

Code:
$ 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.
# 10  
Old 10-16-2008
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
# 11  
Old 10-16-2008
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
# 12  
Old 10-16-2008
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.
# 13  
Old 10-16-2008
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
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