Sort the resulting lines. Remove any duplicate lines. Pass the output to the character translation program tr . The -d ' ' tells tr to delete all spaces. All we have to do is to translate each space into a comma.
Uniq Cats Call the file that contains the possibly unsorted list of users list.of.users .
Tr translates commas into linefeeds, turning our list into a table with one name per line. We then sort the names and discard duplicates. Finally, tr takes the table, and replaces every linefeed with a comma, producing the desired list. The latter two solutions produce lists with an extra comma at the end. You can get rid of this comma using an editor.