List all regex matches, one per line of output

0

I know that I can find regex matches using egrep.

My need is a bit more esoteric:

I need to list all regex matches, one per line of output.

So, assume a file containing the following:

This is just some sample 1text to illustrate the 2text seeker I had in mind.
As you can see in this 3text, I have many 4text matches, more than one 5text match per line of 6text.

With a regex of /[0-9]+text/, I want the output to be:

1text
2text
3text
4text
5text
6text

Short of cobbling up a program to do just that, is there any way I can generate the wanted output using standard commands?

pepoluan

Posted 2016-06-25T05:44:52.267

Reputation: 962

Answers

1

egrep -o '[0-9]+text' filename

From the egrep man page:

-o, --only-matching

Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Mark Riddell

Posted 2016-06-25T05:44:52.267

Reputation: 652

Oh. My. Gosh. How could I missed that?! Thanks! – pepoluan – 2016-06-25T11:38:22.997