Answers for "grep first match"

1

grep first occurence for many matches

Just use flag -m to define number of occurrences to match:
grep -m1 pattern file
Posted by: Guest on October-22-2020
2

bash grep only return first match

# Basic syntax:
grep -m 1 "pattern" input_file.txt
# Where -m is the maximum number of matching lines to return, i.e. stop
#	reading the file after m matches
# Note, this is more efficient than piping to head because there you
#	always read the whole file even if you're only looking for m matches
Posted by: Guest on April-26-2021
0

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
Posted by: Guest on November-26-2020
0

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
Posted by: Guest on October-24-2020

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language