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-04-2009, 08:49 PM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Rep:
|
Small question with the sort command
Well hello, I'm new to the forums and I'm taking a course for Linux and I've gotten stuck on one question, just wondering if I could get some help
Anyway the question goes like this:
The file 'numbers' contains a list of numbers. Write a command to place the
largest one of those numbers in the file 'largest' (there should be nothing
else in that file). Do not use the 'head' command in your answer.
So to make that easier to read:
-Sort a file into numeric order largest to smallest
-Take the largest number from that list, and put it into another file
-Do not use the head command (darn)
So sort is obviously easy enough (sort -n) but I'm having trouble figuring out how I would take the first line of the file (which will be the largest due to the sort) and place it into another file, I'm sure it will end up being something like 'sort -n numbers | Copy line 1 > largest' but I'm not exactly sure how I'd go about doing this, I've done a lot of research and I can only find commands to move whole files/directories, so I'm wondering if you guys could help me out. Thanks.
|
|
|
12-04-2009, 08:58 PM
|
#2
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by Bleek
Do not use the 'head' command in your answer.
|
How about the tail command.
|
|
|
12-04-2009, 08:59 PM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
How about the tail command.
|
I'm not given the size of the list (it's all done in a program) so I can't use tail -# can I?
|
|
|
12-04-2009, 09:02 PM
|
#4
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by Bleek
I'm not given the size of the list (it's all done in a program) so I can't use tail -# can I?
|
Read the man page for tail. I think you're confused about what the options to tail do.
Quote:
Originally Posted by Bleek
The file 'numbers' contains a list of numbers. Write a command to place the
largest one of those numbers in the file 'largest' (there should be nothing
else in that file). Do not use the 'head' command in your answer.
|
That was the assignment, right?
Quote:
So to make that easier to read:
-Sort a file into numeric order largest to smallest
-Take the largest number from that list, and put it into another file
-Do not use the head command (darn)
|
But that was just your thoughts (not part of the official assignment), right?
Last edited by johnsfine; 12-04-2009 at 09:05 PM.
|
|
|
12-04-2009, 09:04 PM
|
#5
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
Read the man page for tail. I think you're confused about what the options to tail do.
|
Ah indeed, I'll have to check that out, I was assuming tail wasn't included in the 'don't use' part because it was un-useable
|
|
|
12-04-2009, 09:07 PM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
There are several commands that will select parts of a file---eg cut or sed.
Your logic might wind up looking like this:
sort <options> filename| sed <commands> > newfilename
Go here for a really good tutorial on sed:
http://www.grymoire.com/Unix/Sed.html
|
|
|
12-04-2009, 09:32 PM
|
#7
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
Read the man page for tail. I think you're confused about what the options to tail do.
That was the assignment, right?
But that was just your thoughts (not part of the official assignment), right?
|
Yes, the second part was just what I 'broke it down' into, the first part is the actual question being asked. So I'm trying to use the tail command and I came up with this:
sort -n numbers | tail -n+1 > largest
Doesn't work, so I'm wondering if you see anything glaringly wrong, I'm going to try looking up a bit more on tail before I try sed... alot of information to look through on sed commands :<
EDIT: I can see why that didn't work now (n doesn't actually reverse it), I've looked through a couple of 'tail command option sheets' and I haven't seen anything that would enable it to do what I want it to...
EDIT2: Okay so now I see that tail +1 will make the file read lines starting at the first one (I think?) so how can I STOP it after the first line?
Last edited by Bleek; 12-04-2009 at 09:51 PM.
|
|
|
12-04-2009, 11:14 PM
|
#8
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Okay so I'm trying some things out and I have some questions.
How do I make 'sort -n numbers | tail +1 > largest' stop at the first line?
My second command, 'sort -n numbers | seq q > largest' why doesn't that work?
|
|
|
12-04-2009, 11:19 PM
|
#9
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
"seq" is for generating a sequence of numbers---it does not make sense to be piping something into it.
If you want the first line of the file, I don't understand why you would use "tail"......What about SED?
|
|
|
12-04-2009, 11:27 PM
|
#10
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
"seq" is for generating a sequence of numbers---it does not make sense to be piping something into it.
If you want the first line of the file, I don't understand why you would use "tail"......What about SED?
|
Gah I meant sed.
sort -n numbers | sed q > largest
That's what I meant.
I also tried:
sort -n numbers | sed -q numbers > largest
sort -n numbers | sed q numbers >> largest
sort -n numbers | sed -q numbers >> largest
None of them worked, is sed q the wrong command to use?
Last edited by Bleek; 12-04-2009 at 11:29 PM.
|
|
|
12-05-2009, 07:52 AM
|
#11
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
What is "sed q" supposed to do? In sed, "q" means quit. Take a look at the man page--or this tutorial:
http://www.grymoire.com/Unix/Sed.html
|
|
|
12-05-2009, 08:12 AM
|
#12
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
How about
Code:
read line <<< $(sort -n numbers)
echo "line is $line"
EDIT: Added third <
Last edited by catkin; 12-05-2009 at 10:18 AM.
Reason: Added third <
|
|
|
12-05-2009, 09:48 AM
|
#13
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by Bleek
How do I make 'sort -n numbers | tail +1 > largest' stop at the first line?
|
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?
|
|
|
12-05-2009, 11:35 AM
|
#14
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
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?
|
What would I change it to? Can't find anything on that >.> I can't see anything to make sed work the way I want it too, apparently it just keeps going and works like a reverse tail, so I'm running into the same issue :<
|
|
|
12-05-2009, 02:10 PM
|
#15
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
I can't see anything to make sed work the way I want
|
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?
|
|
|
All times are GMT -5. The time now is 12:04 AM.
|
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
|
|