Linux - NewbieThis 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.
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.
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.
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?
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?
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?
"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?
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 :<
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.