13

Suppose i have a file with many words, i want to find only the first word with the pattern "xyz". How do I do it if there are multiple words with this pattern on the same line? -m returns all the words in the first line in which it matches. I need only grep command.

2
  • Do you have many lines in your file? Do you want to find the first word in whole file or the first word in each line? Commented Apr 12, 2017 at 11:54
  • 1
    it looks like you're asking people to provide you the full command.That's not really how unix.stackexchange works. You should show what you've done, and where you're stuck Commented Apr 12, 2017 at 12:02

2 Answers 2

20

By default grep prints the lines matching a pattern, so if the pattern appears one or more times into a line, grep will print that whole line.

Adding the flag -m 7 will tell grep to print only the first 7 lines where the pattern appears.

So this should do what you want (I haven't tested it):

grep -o -m 1 xyz myfile | head -1

Edit: as pointed out by @Kusalananda, you don't strictly need the -m flag but using it means grep won't need to parse the whole file, and will output the result faster, especially if myfile is a large file.

5
  • Okay. If I assume there is only one such word per line, grep -o -m 1 "exp" filename would do my job right? Commented Apr 12, 2017 at 11:40
  • 2
    @AtchyutSreekar No, that would'nt, it would fetch all the matching patterns from te first line. You need to pipe that to head -1 to get the real first. Commented Apr 12, 2017 at 11:54
  • 6
    The -m 1 is not needed. The matches returned by -o will be on separate lines and head -1 will pick the first match. Removing -m 1 will make grep go through the entire file (or stop when it notices that it can't write to stdout any longer because head has exited), but it make it portable to other Unices that don't have the -m flag (BSD). Commented Apr 12, 2017 at 12:22
  • @Kusalananda , thank you! May I ask a reference regarding the stream check in the pipe you mentioned, preferably from the view of C/Kernel/lower level, if possible? Commented Nov 25, 2024 at 15:11
  • 1
    @Artfaith You mean grep terminating because head exited? That's standard behaviour. The left-hand process will receive a PIPE signal and will (by default) terminate when it tries to write to a pipe no one is reading from. See the pipe(7) manual. Commented Nov 25, 2024 at 16:39
-1

The answer to your question is in the grep man page:

grep -m1 'searchstring' file_name

The -m<number> option is the key. -m1 will only show the first match, -m2 the first 2 occurrences and so on.

3
  • 3
    it wont find the first occurrence it would find the first line with the first occurrence. Commented Apr 12, 2017 at 12:11
  • yes indeed you do have right. Commented Apr 12, 2017 at 12:12
  • You mean if the first line matches two times, -m2 will only output one line ? Commented Dec 23, 2024 at 9:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.