While loop seems to exit when blank line is met


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers While loop seems to exit when blank line is met
# 1  
Old 05-14-2010
While loop seems to exit when blank line is met

Hello everyone!

I am having an issue with a script I am trying to create.

I have an input file (named sort_$1.txt) like:

Code:
aaaa
bbbb
cccc
 
dddd
eeee

and I process it with the following code:

Code:
while read -r EachLine2
do
   #use grep to get the a value linked to the code in each line, example
   #aaaa would return "SouthWest"
 
   RESULT=$(grep $EachLine2 $DATA_DIR/cells*.dat | cut -d, -f2)
   #If value is empty set to unknown
   if [ -z "$RESULT" ]; then
           echo "$EachLine2""\t"="\t"Unknown >> lcells_names_$1.txt
   #Else echo normally
   else
           echo "$EachLine2""\t"="\t""$RESULT" >> lcells_names_$1.txt
   fi
done < lcells_$1.txt

this produces the following output:


Code:
aaaa  =     "SouthWest"
bbbb  =     "East"
cccc  =     "East"
      =     Unknown

So the problem is that when the script reaches the empty line, it does it's job normally but instead of continuing with the next line, it exits.

I need it to reach the end of file instead, giving me the output:

Code:
aaaa  =     "SouthWest"
bbbb  =     "East"
cccc  =     "East"
      =     Unknown
dddd  =     "...."
eeee  =     "...."

Any ideas?
# 2  
Old 05-14-2010
Which shell are you using? bash read sometimes returns false for the very last line if it's not terminated with a newline. You can code around this. As an aside, you can simplify your script a little too:

Code:
while true
do
  EachLine2=""
  read EachLine2
  [ -z "${EachLine2}" ] && break

   RESULT=$(grep $EachLine2 $DATA_DIR/cells*.dat | cut -d, -f2)
   #If value is empty set to unknown
   if [ -z "$RESULT" ]; then
           echo "$EachLine2""\t"="\t"Unknown 
   #Else echo normally
   else
           echo "$EachLine2""\t"="\t""$RESULT"
   fi
done < lcells_$1.txt  >> lcells_names_$1.txt

# 3  
Old 05-17-2010
Hey Corona!

Thank you for your prompt respose. Unfortunately I was out of country for the weekend and couldn't test your code and reply.

Anyway, the code produces almost the same results. Now instead of getting the 1st empty line, outputing "Unknown" and then exiting, it exits when the first empty line is met.

This means I not get the followign result:

Code:
aaaa  =     "SouthWest"
bbbb  =     "East"
cccc  =     "East"

So the line

Code:
[ -z "${EachLine2}" ] && break

didn't help...

Any more ideas? I'll keep on testing and trying myself. If I get around this, I'll post Smilie
# 4  
Old 05-17-2010
Assuming you are using bash, find out the exit status of this line:

Code:
echo "$EachLine2""\t"="\t"Unknown >> lcells_names_$1.txt

by echoing $? directly after it.

I think because EachLine2 is effectively null - this line may be giving an error status. The bash man page states:

Quote:
The while command continuously executes the do list as long as the last command in list returns an exit status of zero.
# 5  
Old 05-17-2010
Quote:
Assuming you are using bash
Sorry for not being clear on this. Yes, I am using bash.

Quote:
find out the exit status of this line
It is 0.


I still believe that the problem is, read recognizing empty rows as the end of file and I can't seem to get it to read past that empty line...
# 6  
Old 05-17-2010
Ok another thing to try -

What is the exit status of the grep line when EachLine2 contains the empty string. I think that it is failing and causing your loop to exit.

Personally I would add three sets of quotes to the line as such:

Code:
RESULT="$(grep "$EachLine2" "$DATA_DIR"/cells*.dat | cut -d, -f2)"


Last edited by steviefordi; 05-17-2010 at 09:36 AM..
This User Gave Thanks to steviefordi For This Post:
# 7  
Old 05-17-2010
Using the following code, given by Corona688:

Code:
while true
do
        EachLine2=""
        read EachLine2
        [ -z "${EachLine3}" ] && break

        RESULT="$(grep "$EachLine3" "$DATA_DIR"/cells*.dat |cut -d, -f2)"
        echo $?

        if [ -z "${RESULT}" ]; then
                echo "$EachLine3""\t"="\t"Unknown
        else
                echo "$EachLine3""\t"="\t""$RESULT"
        fi
done < lcells_$1.txt >> lcells_names_$1.txt

And the test data being

Code:
aaaa
bbbb
cccc
 


dddd
eeee

I get

Code:
0
0
0

which is the result of the first 3 rows. The next rows never get processed due to:

Code:
 [ -z "${EachLine3}" ] && break

ending the loop before the values of the rest of the lines are processed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

3. Emergency UNIX and Linux Support

For loop exit

Below for loop not exiting. Can someone help? JBOSS_INST_ARGS=01 02 if ; then for i in $JBOSS_INST_ARGS; do /u/jboss-6.1.0.Final/bin/jboss_init_wise$i.sh start; done (8 Replies)
Discussion started by: vino_hymi
8 Replies

4. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

5. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

6. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

7. Shell Programming and Scripting

Exit from loop

hi, how to exit from "if" loop?actually i have mutliple "if" conditions, i have to exit from each "if" loop,if it is true...:confused: Please suggest me... (3 Replies)
Discussion started by: sreelu
3 Replies

8. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

9. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies

10. Shell Programming and Scripting

while loop exit

i wrote a while script as part of a huge program. this script, once picked, begins to output data to the person using it. pretty easy, as the person doesn't have to keep typing commands to get the output that the while loop automatically throws out. now, the thing is, while this while-script... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question