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
# 8  
Old 05-17-2010
Even though read seems to work (for me, that is), you may want to use sed instead (ugly, but possibly more reliable):

Code:
#!/bin/bash

cat test.file | while read LINE
do
  echo $LINE
done

echo "="

LC=$( cat test.file | wc -l )
for (( I=1 ; I<=$LC ; I++ ))
do
  sed -n -e "$I"p test.file
done

exit 0

Code:
[house@leonov] cat test.file
some

text
[house@leonov] bash test.bash
some

text
=
some

text

# 9  
Old 05-17-2010
The test data posted contains one line which contains just one space character. The line is not blank.

This line of script is likely to be the problem:

Quote:
RESULT=$(grep $EachLine2 $DATA_DIR/cells*.dat | cut -d, -f2)
The variable $EachLine2 is not quoted. If it contains a space character or nothing it effectively disappears.
Where $DATA_DIR/cells*.dat expands to more than one filename the grep is still syntactically correct even if $EachLine2 is blank or spaces in which case the first filename gets treated as a regular expression by grep !

To see what $DATA_DIR/cells*.dat expands to, try a plain echo on the variable.
# 10  
Old 05-17-2010
Can't use that loop for some reason... I get

Code:
for ((i=1;i<=$LC;i++))
do
  sed -n -e "$i"p cells_$1.txt
done

Syntax error at line 35 : `((' is not expected.
# 11  
Old 05-17-2010
My apologies haaru, when I gave you the grep line to quote I meant it to be on the code you posted originally.

I understand that other people helping you has probably led you down other routes now - but I'm positive if you quote the grep line as I showed you, on the code you originally posted, it will work.
# 12  
Old 05-17-2010
Quote:
My apologies haaru, when I gave you the grep line to quote I meant it to be on the code you posted originally.

I understand that other people helping you has probably led you down other routes now - but I'm positive if you quote the grep line as I showed you, on the code you originally posted, it will work.
No worries steviefordi!

I think that this is getting somewhere now..

Code:
while read -r EachLine2
do
        RESULT="$(grep "$EachLine2" "$DATA_DIR"/cells*.dat | cut -d, -f2)"
        echo $? >> check.txt
        #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

feeding it 103 rows of data (sample below):

Code:
aaaaa
bbbbb
bbbbb
 
 
 
ccccc
ccccc
ddddd
ddddd

gave 572411 rows of results!! (sample below):

Code:
aaaaa   =       "Correct_Value"
bbbbb   =       "Correct_Value"
bbbbb   =       "Correct_Value"
        =       "Some_Incorrect_data"
"Some_Incorrect_data"
"Some_Incorrect_data"
"Some_Incorrect_data"
"Some_Incorrect_data"
"Some_Incorrect_data"
"Some_Incorrect_data"


I noticed:
  • For the first 3 values fed we get correct results in the correct pattern
    <<Input>> = <<Output>>
  • When the first empty row of data arrives, we get incorrect data for that result but we still get the correct output pattern.
  • For every result after that we get Incorrect data tottaly ignoring the pattern posting only the result.
The output of the program consisted only of exit status 0.


The loop successfuly iterated 103 times, did a wc -l on check.txt file that has the exit status, ( Smilie ) but I am led to believe that after the first empty row it was either:
  1. Fed the whole list of input numbers for each iteration.
  2. Grepped everything from the resultset since the input string was "".
I favor No.2


I think that I will get to the bottom of this, thanks to your help, by just changing the grep command format!

---------- Post updated at 05:50 PM ---------- Previous update was at 05:02 PM ----------

Quote:
Originally Posted by steviefordi
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)"


Problem solved! Thanks everyone and especially steviefordi!!

I used the above plus a more demanding grep command ( grep "\"[A-Z]"$EachLine2"\"" ) to get the correct results

Last edited by haaru; 05-17-2010 at 11:20 AM..
 
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