Display sorted list using sed command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Write a command line that will display a sorted list of logged in users. Just display the username and no other information. To accomplish this task you must use the sed command (The cut command is not allowed).

  2. Relevant commands, code, scripts, algorithms:
    Must use the sed command.

  3. The attempts at a solution (include all code and scripts):
    I am not sure how to even go about this question. Still really new at shell scripting.

  4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Calvin College, Grand Rapids MI, USA, Johnson, CO 142

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Share your thoughts on solving this question? Show us what have you tried so far?

1 Like

For starters I have been just trying to figure out the formatting of using the sed command with the who command. I have been trying w | sed and I am getting a weird result. Im guessing im barking up the wrong tree by trying to pipe w with sed. I was not able to find any similar examples in our book "Unix Shell Programing".

---------- Post updated at 12:20 PM ---------- Previous update was at 12:11 PM ----------

would it make a difference if i used sed | who ?

I would recommend to use who instead of w because then you don't have to worry about skipping the header lines that is present in w output.

You have to pipe who output to sed , use regular expression to remove every character followed by first field (use blank space as delimiter).

Finally pipe the result to sort command to get a sorted list.

Something like:

who | sed your_regexp_here | sort
1 Like

Wow, thank you so much for the help. That really clears some things up for me. I really appreciate it.

---------- Post updated at 10:01 PM ---------- Previous update was at 01:37 PM ----------

so this was what my answer was.

[kofine05 ~]$ who | sed 's/ .*$//' | sort
anthonyl
darnella
kevinbis
robertom
root
[kofine05 ~]$

does that make sense?

Yes it does make sense. Good work!

By the way if there are multiple sessions from same user, you can use sort to get rid of duplicates. Check the sort man pages to understand how to get unique values.

man sort
1 Like