LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Small question with the sort command (https://www.linuxquestions.org/questions/linux-newbie-8/small-question-with-the-sort-command-773578/)

Bleek 12-04-2009 08:49 PM

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.

johnsfine 12-04-2009 08:58 PM

Quote:

Originally Posted by Bleek (Post 3780425)
Do not use the 'head' command in your answer.

How about the tail command.

Bleek 12-04-2009 08:59 PM

Quote:

Originally Posted by johnsfine (Post 3780433)
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?

johnsfine 12-04-2009 09:02 PM

Quote:

Originally Posted by Bleek (Post 3780435)
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 (Post 3780425)
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?

Bleek 12-04-2009 09:04 PM

Quote:

Originally Posted by johnsfine (Post 3780439)
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

pixellany 12-04-2009 09:07 PM

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

Bleek 12-04-2009 09:32 PM

Quote:

Originally Posted by johnsfine (Post 3780439)
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?

Bleek 12-04-2009 11:14 PM

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?

pixellany 12-04-2009 11:19 PM

"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?

Bleek 12-04-2009 11:27 PM

Quote:

Originally Posted by pixellany (Post 3780530)
"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?

pixellany 12-05-2009 07:52 AM

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

catkin 12-05-2009 08:12 AM

How about
Code:

read line <<< $(sort -n numbers)
echo "line is $line"

EDIT: Added third <

johnsfine 12-05-2009 09:48 AM

Quote:

Originally Posted by Bleek (Post 3780527)
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?

Bleek 12-05-2009 11:35 AM

Quote:

Originally Posted by johnsfine (Post 3780921)
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 :<

pixellany 12-05-2009 02:10 PM

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 03:14 PM.