LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 03: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, 03:35 PM   #17
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 04:59 PM   #18
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 07:16 PM   #19
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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, 07: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 07:22 PM.
 
Old 12-05-2009, 07:32 PM   #21
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 07:35 PM.
 
Old 12-06-2009, 04:25 AM   #22
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 05:49 AM   #23
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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 08:50 PM
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 02:17 PM
Sort Command saravanan1979 Programming 1 10-03-2004 12:36 PM
The SORT command Rezon Programming 2 10-30-2003 05:14 PM


All times are GMT -5. The time now is 04:13 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration