Looks like you've got two problems in your script - obviously both with the line while [ $j -le ${problem_arr[@]} ]
First: ${problem_arr[@]} will yield ALL array members and the shell should complain when trying to compare. I guess you want it to read ${#problem_arr[@]} which yields the count of members in array.
Second: You correctly start the loop at j=0 , but you run it one too far up - ${problem_arr[${#problem_arr[@]}]} will be empty and thus sed complains. make the comparison [ $j -lt ... ] and it will fly.
Thanks RudiC for your response.
I verified the points you have highlighted, and there is no problem with array size & initialization.Anyways, i can assign array size to another variable and use it in while condition.
I'd want you just focus on sed part of it, as it throws error there.
Ouch. Yes, you're right. Both bash and ksh use the same notation for setting an indexed array, but bash uses 0 based indexing and ksh uses 1 based indexing. (I guess you can tell that I do most of my shell scripting with ksh. :rolleyes:)
Does it? I'm not sure I understand what you are doing there. In the while loop, you say you are populating array arr from a file. How do you know when to terminate the loop, and what do you expect from the ${#arr$} construct? And how do you assign an element from a file to member arr[$i]?
problem_arr=`grep -E -o "PRB[0-9]{1,4}" problemid.txt`
j=1
while [ $j -le ${problem_arr[@]} ]
do
k=`expr $j + 1`
sed -n "/${problem_arr[$j]}/,/${problem_arr[$k]}/p" problemid.txt >xyz.txt
< so many operation here like between two problems there will be so many states like New, Inprogress, Resolved, closed & date. I have to do opeartions to calculate the time spent in each state for each problem...>
j=`expr $j + 1`
done
You say (post #8) that you are using bash. Array indexing in bash starts from 0 and counts up to ${#arr[@]} - 1 , as stated earlier. In both your posts #8 and #10, you are starting the while loop for array indexing at 1 (i=1 or j=1). So you will be missing the first element in your problem_arr! And in either post you are NOT using the correct method to evaluate the number of elements in the array, which I duplicated above. Btw, the way you assign your problem_arr will not work, as it does not result in an array but in a single variable with n space separated words.
Really Sorry RodiC i couldn't exactly copy pasted my script here coz the script lies in the system where this site is blocked. I'd need to make hell lot of effort to exactly copy paste code. So i just tried to simulate the problem here above.
Yes, You are right! so many things which i abstracted here considering those are kind of implicit.
And i haven't missed any element if i have assigned elements from index 1 to size of array. However, if i had used indexing from 0, i would have limit loop to size-1.
Anyways, appreciate your effort to come forward for understing the real problem. You are simply awesome!!