LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2009, 02:21 PM   #16
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by pixellany View Post
I seem to remember you were just looking for a way to print the first line.... How about:
sed -n '1p'

Did you look at the sed instructions?
I did, but there's ALOT to look through... I tried:
sort -n numbers | sed -n 1p numbers > largest
sort -n numbers | sed -n 1p > largest

Neither worked... I'll keep trying!
 
Old 12-05-2009, 02:35 PM   #17
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I created a file (num) with 5 numbers.
Code:
[mherring@Ath play]$ more num
34
5
6
9
15
[mherring@Ath play]$ sort -n num
5
6
9
15
34
[mherring@Ath play]$ sort -n num|sed -n '$p'
34
[mherring@Ath play]$ sort -r -n num|sed -n '1p'
34
One example prints the LAST line
The second example does a reverse sort and prints the FIRST line.

You will find all this in the man pages and other documents.
 
Old 12-05-2009, 03:59 PM   #18
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
Quote:
Originally Posted by Bleek View Post
I did, but there's ALOT to look through... I tried:
sort -n numbers | sed -n 1p numbers > largest
sort -n numbers | sed -n 1p > largest

Neither worked... I'll keep trying!
Out of curiosity: what WERE the results of those commands
of yours, what did you get in the file 'largest'? I would
have expected the last one to work.


Cheers,
Tink
 
Old 12-05-2009, 06:16 PM   #19
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by johnsfine View Post
There is an old joke about needing instructions on the bottom of a beer bottle saying "open other end".

Have you considered changing your sort command?
Quote:
Originally Posted by Bleek View Post
What would I change it to?
I didn't think it needed further explanation.

Sort so the largest value is the last line. Pipe that to tail to copy just the last line to the destination file.
 
Old 12-05-2009, 06:21 PM   #20
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Out of curiosity: what WERE the results of those commands
of yours, what did you get in the file 'largest'? I would
have expected the last one to work.


Cheers,
Tink
Well the program won't allow me to tinker with its insides (it's basically a program where you write in the answer and won't let you do anything else). Anyway I made a file to try this out myself, within numbers I had:
8
32
21
31
500
43

And when I used 'sort -n numbers | sed -n 1p > largest' it ended up being exactly the same.


When I used 'sort -n numbers | sed -n 1p > largest' the result in 'numbers' was 8. So it seems that my sort command isn't working, which makes no sense to me as it looks fine. Any idea on what's wrong?
Quote:
Originally Posted by johnsfine View Post
I didn't think it needed further explanation.

Sort so the largest value is the last line. Pipe that to tail to copy just the last line to the destination file.
Sadly it seems I have an inherit need to make things as complicated as possible, I'll try that out.

Last edited by Bleek; 12-05-2009 at 06:22 PM.
 
Old 12-05-2009, 06:32 PM   #21
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
And when I used 'sort -n numbers | sed -n 1p > largest' it ended up being exactly the same.
I feel like I have stepped into the twilight zone.....

sort -n puts the largest value last. So sed '1p' will give you the smallest value. Is your system somehow different???

EDIT---maybe "sort" is aliased to something else????

Last edited by pixellany; 12-05-2009 at 06:35 PM.
 
Old 12-06-2009, 03:25 AM   #22
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
Quote:
I feel like I have stepped into the twilight zone.....

sort -n puts the largest value last. So sed '1p' will give you the smallest value. Is your system somehow different???

EDIT---maybe "sort" is aliased to something else????
Nuh .. it's all good, we're both blind :}

It does exactly what we tell it to. Sort gives him 500 as the
last, but with sed we print the first line only, which will be
the smallest number - 8 in his case. The "trick" would be to
actually do the $p rather than 1p ;}


Cheers,
Tink
 
Old 12-06-2009, 04:49 AM   #23
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Code:
read largest <<< $(sort -nr numbers)
echo $largest > largest
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort command arya6000 Linux - Newbie 2 11-27-2007 07:50 PM
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 01:17 PM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration