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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
12-05-2009, 02:21 PM
|
#16
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
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!
|
|
|
12-05-2009, 02:35 PM
|
#17
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
|
12-05-2009, 03:59 PM
|
#18
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by Bleek
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
|
|
|
12-05-2009, 06:16 PM
|
#19
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by johnsfine
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
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.
|
|
|
12-05-2009, 06:21 PM
|
#20
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
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
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.
|
|
|
12-05-2009, 06:32 PM
|
#21
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
|
12-06-2009, 03:25 AM
|
#22
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
12-06-2009, 04:49 AM
|
#23
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Code:
read largest <<< $(sort -nr numbers)
echo $largest > largest
|
|
|
All times are GMT -5. The time now is 02:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|