0

I am trying to sort a file called "Team_James" numerically by the last column, and then I would like to save the output as a new file called "file3".

I tried the command sort Team_James | > s3, but I don't know how to do it, and I don't even know what -k is! I have been looking everywhere on the Internet.

What am I doing wrong?

Here what the output of my command cat Team_James

Pam Murray      4    657-4324  18
Jim Brown       8    467-8743  22
Jim Roberts     17   728-8295  9
Joyce Murray    7    235-1432  13
  • try sort -k 4,4n Team_James >file3 – steve Oct 3 '18 at 21:25
  • Hello @Anonymous what is the expected output? – user88036 Oct 3 '18 at 21:25
  • Hello @Goro trying to get the last field into numeric order – Holy Gaming Empire Oct 3 '18 at 21:28
  • Sorry @steve but it didn't work. Trying to get the last field in numeric order. – Holy Gaming Empire Oct 3 '18 at 21:31
  • 1
    Might you have a player with a middle name listed? – Jeff Schaller Oct 3 '18 at 21:38
1

Looking at the table, in column#1 there are spaces between first name and last name. This means, sort will consider the first and last names two separate columns. As a result, the last column of the input table is #5. The command below, assume that all the names provided in column #1 are first/last and there are no middle names.

cat Team_James | sort -s -n -k 5.1 > file3
Jim Roberts     17   728-8295  9
Joyce Murray    7    235-1432  13
Pam Murray      4    657-4324  18
Jim Brown       8    467-8743  22
-n, --numeric-sort   compare according to string numerical value  
-s, --stable         stabilize sort by disabling last-resort comparison  
-k, --key=POS1[,POS2]  
      start a key at POS1 (origin 1), end it at POS2 (default end of  
      line).  See POS syntax above.  
  • Yes, But once I've done that I need to send it to file3 – Holy Gaming Empire Oct 3 '18 at 21:34
  • @Anonymous what do you mean send it to file3? By email? or save it in a new file called file3? If yo mean save the output as file3 please see the edits. if by email I can revise again ;-) – user88036 Oct 3 '18 at 21:37
  • 1
    Thank you for solving the problem I was stuck at this for a while. – Holy Gaming Empire Oct 3 '18 at 21:40
  • You are welcome ;-) see k explaination – user88036 Oct 3 '18 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.