grep 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

3.67
9

                                    Just use flag -m to define number of occurrences to match:
grep -m1 pattern file

3.67 (9 Votes)
0
4
3

                                    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

4 (3 Votes)
0
4.33
3
DavidB 95 points

                                    #Combine grep and tail for grepping until second match and filtering 
#last with tail command
grep -m2 "two" in-file.txt | tail -n1

4.33 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
grep first match in line how to grep only the first match grep get only first match first match of string using grep grep find first occurence grep match first word grep print only first match line grep print only first match regex grep get first match grep second occurrence first match grep grep Eo only first match grep first matching grep only the first match grep return first match bash grep only return first match grep extract first match grep invert match only first match grep only return first match grep command to take the first match grep first occurence grep first search grep the first match gow to grep any only return value after item serching for making grep search first grep exact match grep get only string that match grep get first match grep 10th occurrence of pattern first result only grep grep find first match get only one row from grep grep single match from end grep single match grep match only one grep take second match grep first match after certain line grep only one match count occurences with grep grep match last two characters grep match first line stop grep stop after first match grep find only one occurrence grep first instance from list of files grep only first instance grep find only second match grep find one grep only first hit how to grep the second occurrence in unix only print 1 copy grep grep only first match how to select only the first match of grep grep third match grep second match grep until first match grep after first match grep first match in file grep first match grep stop at first match get first response from grep linux grep first occurance grep first occurrence grep 1st occurance bash grep first match grep regex first match grep first element of list grep first element get the first element from grep output grep first occurence for many matches
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source