Hi,

Following is my code :

i=3
j=3
while [ $i -le $count1 ]
do
i=`expr $i + 3`
head -n 60 file1.imp >> s27.txt
while [ $j -le 60 ]
do
head -n 3 file2.imp >> s27.imp
./prog1 s27.txt > result.txt
if grep OUT result.txt
then
head -n 3 file1.imp >> final.imp
num=`expr $num + 1`
else
k=`expr $j -3`
sed "$j,$k d" file1.imp > f1.imp
mv f1.imp file1.imp
fi
sed '1,3d' file2.imp > f2.imp
mv f2.imp file2.imp
j=`expr $j +3`
done

I am checking 3 lines from file2 at a time.

I am getting the following error in while loop and sed command. I am trying to delete 3 lines from the file1.

Error: expr: syntax error
sed: 1: "3, d": expected context address
expr: syntax error
line 21: [: -le: unary operator expected

Can I use a while loop inside a while loop. Is there something wrong in my syntax for sed and while.

Please help.

Thanks

Dani AI

Generated

A few concrete causes explain the errors you saw. An unset or empty variable in an arithmetic test produces the expr: syntax error, and that often cascades into a malformed sed address (for example sed seeing 3, d when the end value is empty). ’s missing-space fix is exactly the kind of syntax bug that triggers this. was also correct to point out the missing done and the need to ensure count1 is initialized.

Checklist to make the loop robust:

  • Initialize all counters (i, j, num, count1) before use. An empty count1 causes the [: -le: unary operator expected message.

  • Quote variables in test expressions: use [ "$i" -le "$count1" ] so an empty value does not break the test.

  • Prefer shell arithmetic over expr. It avoids spacing gotchas and is clearer:

    j=3
    while [ "$j" -le 60 ]; do
      # work...
      j=$(( j + 3 ))
    done

Sed-range specifics and a safe pattern:

  • Sed expects a numeric start,end with no stray spaces before d. If end is empty or less than start, sed will complain.

  • Guard the delete with a sanity check before calling sed:

    start=$j
    end=$(( j - 3 ))
    if [ -n "$end" ] && [ "$start" -le "$end" ]; then
      sed "${start},${end}d" file1.imp > tmp && mv tmp file1.imp
    fi

Additional hardening and debug tips:

  • Reinitialize the inner-loop index at the start of each outer loop iteration so it doesn’t carry over stale values.
  • Use set -u (treat unset vars as errors) and set -x to trace execution while debugging.
  • Test sed and arithmetic on a small copy of the files first, and echo key variables before running destructive commands.

These checks prevent the common failure modes that produced the errors in this thread.

Recommended Answers

All 4 Replies

Hey There,

I'll check this out. What are you trying to delete and what are the spec's for deletion? For the code you posted, you're missing a closing "done" for the outside while loop. It's perfectly okay to do a while loop within a while loop, I'm just thinking it might be easier to do this in a different way.

Thanks,

Mike

Sorry,

Looks like the count1 variable isn't set either and it's used in a arithmetic test right at the beginning. If possible, could you cut and paste the entire code and post it in code-tags. It seems that the entire program isn't here.

Thanks :)

, Mike

I got the mistake. There was a space missing in the expression j = j+ 3.

Thanks guys...

Good catch :) Those spaces'll getcha every time, especially since expr is different than "let" arithmetic and I often get the two confused :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.