If I get you correct than for:
$ who
oxo tty7 2014-05-12 14:32 (:0)
bar pts/5 2014-05-12 18:35 (:0:S.1)
oxo pts/1 2014-05-13 13:29 (:0:S.5)
baz pts/8 2014-05-12 18:35 (:0:S.2)
oxo pts/12 2014-05-12 18:35 (:0:S.3)
oxo pts/13 2014-05-12 18:35 (:0:S.4)
foo pts/15 2014-05-12 18:35 (:0:S.0)
bar pts/17 2014-05-13 19:36 (:0:S.6)
bar pts/18 2014-05-14 00:03 (:0:S.7)
you expect to get for example:
$ ./who.sh foo bar
bar pts/5
foo pts/15
bar pts/17
bar pts/18
If so than this will work for you:
#!/bin/bash
users=`echo $@|tr " " "|"`
who|sort -k 3|awk -v users="$users" '$1 ~ users {print $1" "$2}'
Actually it could be done just in the awk
witout any tr
or sort
but I hope it's good enough.
UPDATE:
To get rid of tr
this can be used:
#!/bin/bash
who|sort -k 3|awk -v users="$*" 'BEGIN { regex = gensub(/\s/, "|" ,"g", users) }; $1 ~ regex { print $1" "$2 }'
You can use asorti()
to replace sort
.