Help in finding the max and min position

Hi,

I have this input file called ttbitnres (which is catenated and sorted):-

8 0.4444 213
10 0.5555 342
11 0.5555 321
12 0.5555 231
13 0.4444 400

My code is at :-

#!/bin/bash 
 
echo -e Version "\t" Number of Pass "\t" Number of Fail "\t" Rank Position "\t"Min "\t" Max "\t"Gap"\t"Overall Rank %"\t\t" Qe"\t\t" Score>BugpercttbitCorr.txt
 
cat file.txt|while read LINE
do
 
version=`echo "${LINE}"| awk '{print $1}'`
 
char=`echo "${LINE}"| awk '{print $2}'`
 
cd ~/siemens/schedule2/newoutputs/$version
#get the last line number plus one because of gcc extra one line at top of the code
last=`cat bitresult.txt|tail -1|awk {'print $1+1'}`
echo $last
#catenate the ttres files to append numbers
cat -n ttbitres.txt>ttbitnres.txt
 
echo $char
echo $version
i=`grep -w  "^$char" bitresult.txt|awk '{print $2}'`
j=`grep -w  "^$char" bitresult.txt|awk '{print $3}'`

q=`echo $i $j |awk '{if($1 == 0 && $2 == 0) print "0"; else printf("%f",($2/($1+$2)))}'`

read min max gap k c < <(awk -v var="$char" -v last="$last" '
 
!found || (found && $2 == score) {
	scores[$1] = $2
}
 
$3 == var {
	score = $2
	k = $1
	found = 1
}
 
END {
 
min=k
	for (i  in scores){
 
		if (scores == score)
		{		                          
		       if (i < min )
				min = i
		       if (i > max)
				max = i
 
		}
       }   
 
    gap=max-min+1;
    if(gap==1)
      c=(k/last)*min*100;    
   else 
      c=(k/last)*((k-min)/gap)*100;
 
print min,max,gap,k,c
 
 
}' ttbitnres.txt)
 
score=$(awk -v "char=$char" '$2 == char { val=$1 }END {print val?val:0 }' ttbitres.txt)

echo -e $version"\t\t\t"$i"\t\t\t"$j"\t\t"$k"\t\t"$min"\t\t"$max"\t"$gap"\t"$c"\t"$q"\t"$score>>BugpercttbitCorr.txt
 
done


What im doing is grab the "position" which is $1 in this file for specified $3(line number), So let say i am intrerested to find out line number 321 score.
Since the score of 321 is 0.5555, I wanted to handle 2 cases.

1st) If there are any other elements before or after whcih have same score,
i need to compare the position ($1) and take the min(first seen minimum position which have the score 0.5555) and take the max(last seen maximum position which have score 0.5555). This should return us position 10 for min
and 12 for max.

2nd) If (in other case) there are no other elements with similar score, i will just take the first and only position which have score of 0.5555 which in this case should return us 11.

I tried to load the above input but it seems not able to compare properly to find the min and max. It returns me 10 as min and "nothing" for max. For your info, I print out as echo -e...

Please advise. Thanks.

Given the provided input file and your instructions, you could split your problem and try something simple, like this:

line=321

score=`awk -v "l=${line}" '$3 == l { print $2 }' input_file.txt`

awk -v "s=${score}" '
   $2 == s {
      n=$1;
      if (!min || (n<min)) min=n;
      if (!max || (n>max)) max=n;
   }
   END {
      print min, max;
   }
' input_file.txt