Finding the max and min values and printing the line from a file

awk, bash, linux, shell, unix

Solution

For min value:

[bash]$ cut -f1 -d"," file_name | sort -n | head -1

For max value:

[bash]$ cut -f1 -d"," file_name | sort -n | tail -1

Problem

I have a file which has numbers at the first column. ``` 100,red 101,blue 102,black ``` I should write a shell script that it will print the line with the maximum and minimum numbers. ``` max=0 cat file.txt|while read LINE do fir=`echo $LINE|awk '{print $2}'` sec=`echo $LINE|awk '{print $3}'` if [ $fir -gt $max ]; then max=$fir fi if [ $sec -gt $max ];then max=$sec fi done grep $max file.txt ``` This is what I tried so far for finding the maximum.

Original source