grep first match code example
Example 1: grep first occurence for many matches
Just use flag -m to define number of occurrences to match:
grep -m1 pattern file
Example 2: grep second match
#Combine grep and tail for grepping until second match and filtering
#last with tail command
grep -m2 "two" in-file.txt | tail -n1
Example 3: grep first match
Just combine grep with head command for filtering only the first match as:
grep "match" | head -n 1 #Change 1 to not only match first but further matches