Ascending & Descending order numbers

Dear All,

I have below attached file in which i have many nos, i want the last ascending order nos. The brief description is given below.

File
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
433
315
381
432
315
381
432
315
381
432
315
381
432
315
381
382
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432
315
381
432

The required out put must be as given below. (Only last ascending nos)

315
381
432

use the below command:

cat filename | sort | uniq | tail -3

Actually the cat is useless, and you can use sort -u to avoid the uniq. You probably want numeric sort anyway.

sort -nu file | tail -n 3

Your example suggests you want the last three numbers (i.e. simply tail -n 3 file), not the sorted output. The last three numbers from an ascending sort would be 382, 432, and 433 from the sample input you provided.

Dear Era,

But the value of numbers are not fixed. But i want to grep only last min to max nos.... please help

Hi Pravani,

try this

val=`tail -1 filename`
\rm temp
val=`tail -1 filename`
echo $val >> temp
j=2; for i in `cat filename`
do
val=`tail -$j filename | head -1 `
if [ $val -ge $val2 ]
then
break
else
echo $val >> temp
val2=$val
j=`expr $j + 1`
fi
done
sort -n temp

Thanks
Penchal

Last three lines, in ascending order?

tail -n 3 file | sort -n

What does grep have to do with this?
And what do you mean by min to max with relation to 'last 3'?

Here's a simple was to get the sorted last 3:
sort -un filename | tail -3

Here's a simple was to get the min and max:
sort -un filename | head -1 ; sort -un filename | tail -1

FYI: The u switch produces unique output, while the n switch sorts numerically (i.e. so 234 comes before 1234).

---

Intersting:

For curiosity, I changed one of the 432 values in the middle to 0432 then tried it and got this result:
sort -un filename
315
381
382
0432
433

I had to use sort WITHOUT the u switch followed by uniq to include both 0432 and 432:
sort -n filename | uniq
315
381
382
0432
432
433