Ascending & Descending order numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ascending & Descending order numbers
# 1  
Old 06-03-2008
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
# 2  
Old 06-03-2008
use the below command:

cat filename | sort | uniq | tail -3
# 3  
Old 06-03-2008
Actually the cat is useless, and you can use sort -u to avoid the uniq. You probably want numeric sort anyway.

Code:
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.
# 4  
Old 06-03-2008
Dear Era,

But the value of numbers are not fixed. But i want to grep only last min to max nos.... please help
# 5  
Old 06-03-2008
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
# 6  
Old 06-03-2008
Last three lines, in ascending order?

Code:
tail -n 3 file | sort -n

# 7  
Old 06-03-2008
Quote:
Originally Posted by pravani1
But the value of numbers are not fixed. But i want to grep only last min to max nos.... please help
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort list of directories in descending order in perl?

Hi, I have a problem . I have few directories like inpTDT_1, inpTDT_2, inpTDT_3 and so on inside HOME directory . In one of my perl script (which is in my HOME), the above directories like inpTDT_1, inpTDT_2, inpTDT_3 are sorting out in an order So I wanted to sort all the inpTDT_1, inpTDT_2,... (1 Reply)
Discussion started by: venkatesh
1 Replies

2. UNIX for Dummies Questions & Answers

Sorting a file in descending order when you have 10e- values

Hi, I am trying to sort the following file in descending order of its fourth column. 2 1 363828 -2.423225e-03 3 1 363828 4.132763e-03 3 2 363828 8.150133e-03 4 1 363828 4.126890e-03 I use sort -k4,4g -r input.txt > output.txt ... (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

Appending a column of numbers in ascending order to a text file

I have a text file where I want to append a column of numbers in ascending orders. Input: 57 abc 25 def 32 ghi 54 jkl Output:57 abc 57 abc 1 25 def 2 32 ghi 3 54 jkl 4 How do I go about doing that? Thanks! (11 Replies)
Discussion started by: evelibertine
11 Replies

4. Shell Programming and Scripting

script for remove descending order number

hi all i want to remove some descending order number example : 1 100 200 135.00 Gk_wirs 1 1 100 200 136.00 Gk_wirs 50 1 110 210 138.00 Gk_wirs 60 1 100 200 136.00 Gk_wirs 57 ----> how to remove... (6 Replies)
Discussion started by: nithyanandan
6 Replies

5. Shell Programming and Scripting

Help with sort data based on descending order problem

Input file 9.99331e-13 8.98451e-65 9.98418e-34 7.98319e-08 365592 111669 74942.9 0 Desired output 365592 111669 74942.9 7.98319e-08 1.99331e-13 6.98418e-34 (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. UNIX for Dummies Questions & Answers

how to list descending order

Hi, I want to short descending all the files according to their size.Please help me.. (1 Reply)
Discussion started by: jyotidas
1 Replies

7. Shell Programming and Scripting

ascending and descending sort

Hi I have a problem with sort command : sort -nk 1.28,1.34 file | sort -nrk 1.27 file | sort -nk 1.22,1.25 file |sort -nk 1.13,1.21 file | sort -nk 1.9,1.12 file | sort -nk 1.1,1.8 file This is the input file 0000000100010000000200004090317003 0000000100010000000230001020592002... (3 Replies)
Discussion started by: Fafa
3 Replies

8. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

9. UNIX for Dummies Questions & Answers

Sort ascending and descending

How can I sort a file as follows ? cols 1 - 10 ascending cols 11 - 18 descending cols 19 - 20 ascending Thanks (1 Reply)
Discussion started by: don_0110
1 Replies

10. UNIX for Dummies Questions & Answers

how to see disk usage in descending order

What is the command used by sysadmin to see the disk used by the users in descending order of their disk usage? (8 Replies)
Discussion started by: asutoshch
8 Replies
Login or Register to Ask a Question