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. |
Quote:
|
Quote:
|
Quote:
Quote:
Quote:
|
Quote:
|
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 |
Quote:
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? |
Quote:
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? |
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 |
How about
Code:
read line <<< $(sort -n numbers) |
Quote:
Have you considered changing your sort command? |
Quote:
|
Quote:
sed -n '1p' Did you look at the sed instructions? |
All times are GMT -5. The time now is 03:14 PM. |