w | tail -n +3 | cut -d ' ' -f1 | sort -u
The w
command shows who is logged on and their login times and currently running programs. We want to extract from this just the username (the first column), and remove duplicates.
The |
(Pipe) symbol makes the shell take the output of the command on the left and use it as the input for the command on the right. In this case, it passes the output of the w
command to the next command in the pipeline.
tail -n +3
removes the first 2 lines from the input. We do this because the first 2 lines of the output of w
are header lines, and we don't need that, just the rest.
cut -d ' ' -f1
splits each line of the input using space as the delimiter (-d ' '
), and then selects field 1 (-f1
). This gives us just the username from the output of w
, which is in the first column.
sort -u
sorts the input lines alphabetically, and with the -u
flag it also removes duplicates.