Hint
Each field in a /etc/passwd line is separated by a colon, and the username is the first field, so you need to filter each line to only show the characters up to the first colon
Answer
grep is not even close to the best tool for doing this, but if you're required to use it, this will work:
grep -oE '^[^:]+' /etc/passwd
-o tells it to only return the part of the line that matches. -E turns on extended regular expressions so the + will work later. ^ matches the beginning of the line, [^:] matches anything except a colon, and + means as many characters as possible. So this will match the beginning of every line up until the first colon
If you're able to use other tools besides grep, here are other generally better ways of doing it:
cut -d: -f1 /etc/passwd
sed 's/:.*//' /etc/passwd
awk -F: '{print $1}' /etc/passwd
You can redirect the results from any of those into allusers using > allusers like you have in your example
greplooks for patterns and normally prints out the whole line, you need to limit its output to just the match. Check the manpage. – vonbrand Feb 19 at 3:54