comparing the values of repeated keys in multiple columns

Hi Guyz
The 1st column of the input file has repeated keys like x,y and z.
The ist task is if the 1st column has unique key (say x) and then need to consider 4th column, if it is + symbol then subtract 2nd column value with 3rd column value (we will get 2(10-8)) or if it is - symbol subtract 3rd column value with 2nd(we ll get 4 (14-10)) after that output the line with low value(2) i.e. x 10 8 + and the same follows to y and z.....etc.

Thnx
input

x   10   8   +      
x   10   14  -     
y   10   8   +     
y   10   11  -   
z   10   8   +   

output

x   10   8   +  
y   10   11  - 
z   10   8   +   
 

This shell script does what you want, but there is probably a better way to solve your problem.

#!/bin/sh

while read line; do
	c1=$(echo "$line" | awk '{print $1}')
	c2=$(echo "$line" | awk '{print $2}')
	c3=$(echo "$line" | awk '{print $3}')
	c4=$(echo "$line" | awk '{print $4}')
	if [ "$c4" = '+' ]; then j=$(($c2-$c3)); else j=$(($c3-$c2)); fi
	[ ! "$p" ] && p="$c1"
	if [ "$p" != "$c1" ]; then echo "$this"; i=""; p=""; fi
	[ -z "$i" ] && i="$j"
	if [ "$j" -le "$i" ]; then i="$j"; this="$line"; fi
done < file
echo "$this"

exit 0

The datafile is called file in this script
From c1... to c4..., we register each field from the datafile file in a variable.
We then check if c4 is a + or - and act accordingly.
p is a variable to remember the last c1, on the first pass, it does not exist, so p = c1
if p != c1 (if we change from x to y or z), we display the line this that we set further in the script and we reset i (used later) and p.
if i does not exist (which is the case on the first pass), we set it to j, calculated above
if j is less than or equal to i, then we set j to i, and we keep the corresponding line from the datafile in the variable this
We go back to the top and check for the next line in the file.

nawk '
  {diff=($NF=="+")?$2-$3:$3-$2; a[$1]=($1 in a)?((diff<=a[$1])?diff:a[$1]):diff;b[$1]=(diff==a[$1])?$0:b[$1]}
  END {
    for( i in b)
      print b
}' myFile

Thanx alot for broad explanation @tukuyomi . Really appreciate your effort.
But Vgersh script is quiet fast though.Thank you both guys