Sort list of currently logged in users and display one line containing the userid that sorts last? Feels like it should be simple, but I'm having trouble coming up with the code for this.

sample for clarification:

user tty2
user pts/0

Sort the list generated by the who command so that the only line being displayed is the last line sorted.

Instead of the above "list" I just want the list sorted and only have the last "sorted" item displayed so like:

user pts/0

This is assuming that is the last line. The other data such as the date and stuff I expect to be included the above shorthand is just an example.

I imagine the code would start off like this:

who | sort

I hope this is not too confusing of an explanation.

Actually I think I just figured this out.

who | sort | tail -n 1
  • Sorts according to what? What is displayed in the one line? Just the userid? Please add sample output to clarify what it is you are asking.
    – jasonwryan
    Oct 27 '15 at 4:59
  • I edited my question. Assuming the use of the who command I'm trying to figure out how to display just the last line sorted from the output of the who command (which is a list of the currently logged on users I think). Oct 27 '15 at 5:03

I got my intended result by using:

who | sort | tail -n 1
  • If you want to sort by the date & time fields from who, use who | sort -k 3,4 | tail -n 1. That will give you the last user who logged in, rather than the lowest sorting username.
    – cas
    Oct 27 '15 at 6:29

Store the sorted list in a file than simply display the last line of that sorted file.

who | sort > out && tail -n 1 out
  • 1
    The file is redundant (and would need to be cleaned up afterwards)...
    – jasonwryan
    Oct 27 '15 at 5:15
  • I posted an answer to my own question would "who | sort | tail -n 1" not be right? Oct 27 '15 at 5:16
  • so it will like this " who | sort > out && tail -n 1 out && rm out "
    – user137124
    Oct 27 '15 at 5:17
  • Right but i have seen that after writing and posting my answer. oops for that
    – user137124
    Oct 27 '15 at 5:18
  • @DavidPrentice, select the tick on your own answer if you think it's better. IMO, it is.
    – cas
    Oct 27 '15 at 6:30

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.