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.
-
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?Arkadiusz Drabczyk– Arkadiusz Drabczyk2017-04-12 11:54:13 +00:00Commented Apr 12, 2017 at 11:54
-
1it 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 stuckModassir Haider– Modassir Haider2017-04-12 12:02:56 +00:00Commented Apr 12, 2017 at 12:02
2 Answers
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.
-
Okay. If I assume there is only one such word per line, grep -o -m 1 "exp" filename would do my job right?Atchyut Sreekar– Atchyut Sreekar2017-04-12 11:40:11 +00:00Commented 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.user218374– user2183742017-04-12 11:54:42 +00:00Commented Apr 12, 2017 at 11:54
-
6The
-m 1is not needed. The matches returned by-owill be on separate lines andhead -1will pick the first match. Removing-m 1will makegrepgo through the entire file (or stop when it notices that it can't write to stdout any longer becauseheadhas exited), but it make it portable to other Unices that don't have the-mflag (BSD).2017-04-12 12:22:02 +00:00Commented 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?Serious Angel– Serious Angel2024-11-25 15:11:18 +00:00Commented Nov 25, 2024 at 15:11
-
1@Artfaith You mean
grepterminating becauseheadexited? 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 thepipe(7)manual.2024-11-25 16:39:35 +00:00Commented Nov 25, 2024 at 16:39
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.
-
3it wont find the first occurrence it would find the first line with the first occurrence.Atchyut Sreekar– Atchyut Sreekar2017-04-12 12:11:00 +00:00Commented Apr 12, 2017 at 12:11
-
yes indeed you do have right.Med Saleh Mdini– Med Saleh Mdini2017-04-12 12:12:53 +00:00Commented Apr 12, 2017 at 12:12
-
You mean if the first line matches two times, -m2 will only output one line ?Johan Boulé– Johan Boulé2024-12-23 09:41:51 +00:00Commented Dec 23, 2024 at 9:41