LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-04-2009, 08:49 PM   #1
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
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.
 
Old 12-04-2009, 08:58 PM   #2
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 Bleek View Post
Do not use the 'head' command in your answer.
How about the tail command.
 
Old 12-04-2009, 08:59 PM   #3
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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?
 
Old 12-04-2009, 09:02 PM   #4
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 Bleek View Post
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 View Post
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.
 
Old 12-04-2009, 09:04 PM   #5
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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
 
Old 12-04-2009, 09:07 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 12-04-2009, 09:32 PM   #7
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 12-04-2009, 11:14 PM   #8
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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?
 
Old 12-04-2009, 11:19 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"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?
 
Old 12-04-2009, 11:27 PM   #10
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
"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.
 
Old 12-05-2009, 07:52 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 12-05-2009, 08:12 AM   #12
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
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 <
 
Old 12-05-2009, 09:48 AM   #13
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 Bleek View Post
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?
 
Old 12-05-2009, 11:35 AM   #14
Bleek
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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?
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 :<
 
Old 12-05-2009, 02:10 PM   #15
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:
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?
 
  


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 07:50 PM
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 01:17 PM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM


All times are GMT -5. The time now is 01:21 AM.

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