The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort ascending and descending don_0110 UNIX for Dummies Questions & Answers 1 11-27-2008 03:59 PM
Sort Descending saint2006 Shell Programming and Scripting 1 05-19-2008 02:20 PM
Sort Numbers in ascending orders Raynon Shell Programming and Scripting 7 01-19-2007 02:03 AM
how to see disk usage in descending order asutoshch UNIX for Dummies Questions & Answers 8 09-18-2001 08:58 PM
Sort / ascending order gyik UNIX for Dummies Questions & Answers 1 03-05-2001 10:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-03-2008
pravani1 pravani1 is offline
Registered User
  
 

Join Date: May 2008
Location: India
Posts: 8
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 (permalink)  
Old 06-03-2008
ali560045's Avatar
ali560045 ali560045 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 328
use the below command:

cat filename | sort | uniq | tail -3
  #3 (permalink)  
Old 06-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 06-03-2008
pravani1 pravani1 is offline
Registered User
  
 

Join Date: May 2008
Location: India
Posts: 8
Dear Era,

But the value of numbers are not fixed. But i want to grep only last min to max nos.... please help
  #5 (permalink)  
Old 06-03-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
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 (permalink)  
Old 06-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Last three lines, in ascending order?

Code:
tail -n 3 file | sort -n
  #7 (permalink)  
Old 06-03-2008
Dave Miller's Avatar
Dave Miller Dave Miller is offline
Registered User
  
 

Join Date: Dec 2007
Location: Northern NJ, USA
Posts: 151
Quote:
Originally Posted by pravani1 View Post
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0