Find the minimum value

Hi there

I have generated a column containing 100.000 values. Sample:

94.971
101.468
73.120
100.601
102.329

I need to find the minimum value in this file and I must know which row it is in (no sorting).

I hope you can help!

Thanks!

Homework?

awk 'END { print min }
{ 
  min || min = $1
  $1 < min && min = NR
  }' infile

---------- Post updated at 07:41 PM ---------- Previous update was at 07:40 PM ----------

I hope not ...

awk 'END { print min, "at line" ,s}
{ 
  min || min = $1
  s || s = NR
  if ($1 < min) {min=$1; s=NR} 
  }' infile

73.120 at line 3

Another one, in perl..

perl -ne 'push(@a,$_); END{ print  "",(sort { $a <=> $b } @a)[0]; }'

The poster need know: "1. minimum value and 2. which row it is in "

Your code has same problem as radoulov's, that's why I update radoulov's code, and paste here.

What's the problem with the code I posted?

Hi

cat -n infile | sort -k2,2n | head -1

Guru

Since this question was posted from an university computer, all replies are set to moderated until it's proven to not be homework.

There is a problem with the script :

awk 'END { print min, "at line" ,s}
{
  min || min = $1
  s || s = NR
  if ($1 < min) {min=$1; s=NR}
  }' inputfile

when the input file contains the value 0 :

94.971
101.468
73.120
0.0
100.601
102.329

The result is

100.601 at line 4

instead of

0.0 at line 4

A new version of the script :

awk 'END { print min, "at line" ,s}
NR==1 {
  min = $1;
  s   = NR;
  next;
}
{
  if ($1 < min) {min=$1; s=NR}
  }' inputfile

Jean-Pierre.

Yes,
mine handles that too ...

I'm working on a project at the Technical University of Denmark! I have just started learning unix and needed to pick the minimum value from a dataset of 100.000 scores of protein structures that I generated using Rosetta!
There is no teaching or homework in Denmark during the summer holidays :slight_smile:

Thank you for all your answers :slight_smile: They were very helpful

try this

# sort -k1n infile | head -1
grep -n $( sort -g data | head -n 1 ) data

Ok, Slightly modified for handling it..

 
perl -ne 'chomp ; push(@a,"$_ at line  $."); END{ print  "",(sort { $a <=> $b } @a)[0]; }' file

OK,
rdcwayx is right, I overlooked the fact that the minimum value was needed as well:

 awk 'END { 
  print v, "on line", nr
  }
{
  NR == 1 && min = $1 
  if ($1 <= min) {
    v = $1; nr = NR
    }
  }' infile  

Hi i write a script :rolleyes:

# cat infile
94.971
101.468
73.120
73.090
73.090
73.090
73.070
73.650
100.601
102.329
 
# ./smallest infile
At line 7 -> Small Value is :  73.070
#!/bin/bash
# Smalletst Finder ygemici
i=0
 while read -r lin ;
   do
     myarray=${lin} ; ((i++))
   done < $1
  mynew=`echo $(echo "${myarray[0]} * 1000" | bc) | sed 's/\.0*//g'`
 
first=0
i=1
   for mycheck in ${myarray[@]}
     do
       mychk=`echo $(echo "${mycheck} * 1000" | bc) | sed 's/\.0*//g'`
     if [ $mynew -ne $mychk ] ; then
        if [ $mynew -gt $mychk ] ; then
           ((first++))
            if (( $first == 1 )) ; then
               smallval=$mychk
            fi
          if (( $first > 1 )) ; then
             if [ ${smallval} -gt $mychk ] ; then
                ((i++))
                smallval=$mychk
                smallval[i-1]=""
             fi
          fi
        fi
     fi
     done
echo "At line" $(sed -n "/$(echo ${smallval[@]}|sed 's:\(...\)$:\.\1:')/=" $1)  "->" "Small Value is : " "$(echo ${smallval[@]} | sed 's/\(...\)$/\.\1/')"

Regards
ygemici