Wednesday, July 27, 2022

[SOLVED] Find every occurrence of a regex in a file

Issue

How can I list every occurrence of a regex in a file, where one line can contain the regex multiple times?

Example:
XXXXXXXXXX FOO3 XXXX FOO4 XXX
XXX FOO2 XXXX FOO9 XXXXXX FOO5

The result should be:
FOO3
FOO4
FOO2
FOO9
FOO5

The regex would be /FOO./ in this case.

Grep will return every line that matches at least one time. But that's not what I want.


Solution

Have you tried the -o option: (grep man page)

-o, --only-matching Show only the part of a matching line that matches PATTERN.



Answered By - Sam
Answer Checked By - Dawn Plyler (WPSolving Volunteer)