How to print only the first match with grep

To show only the first match with grep, use -m parameter, e.g.:

grep -m1 pattern file

-m num, --max-count=num

Stop reading the file after num matches.


If you really want return just the first word and want to do this with grep and your grep happens to be a recent version of GNU grep, you probably want the -o option. I believe you can do this without the -P and the \b at the beginning is not really necessary. Hence: users | grep -o "^\w*\b".

Yet, as @manatwork mentioned, shell built-in read or cut/sed/awk seem to be more appropriate (particularly once you get to the point you'd need to do something more).