Grep only the first match and stop
grep
Solution
`-m 1` means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed.
You can use `head -1` to solve this problem:
grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/dir | head -1
explanation of each grep option:
-o, --only-matching, print only the matched part of the line (instead of the entire line)
-a, --text, process a binary file as if it were text
-m 1, --max-count, stop reading a file after 1 matching line
-h, --no-filename, suppress the prefixing of file names on output
-r, --recursive, read all files under a directory recursively
Problem
I'm searching a directory recursively using grep with the following arguments hoping to only return the first match. Unfortunately, it returns more than one -- in-fact two the last time I looked. It seems like I have too many arguments, especially without getting the desired outcome. :-/ ``` # grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/directory ``` returns: ``` Pulsanti Operietur Pulsanti Operietur ``` Maybe grep isn't the best way to do this? You tell me, thanks very much.