Grep only the first match and stop

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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.

Answers

-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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/14093452/grep-only-the-first-match-and-stop

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils