Sam
Sam

Reputation: 2331

Grepping for a pattern followed by another pattern and excluding what lies inbetween as ouput

I want to do something like

egrep -o '(mon|tues)[1-3]?[0-9].*(mon|tues)[1-3]?[0-9]'

And only get what isn't found by the (mon|tues)[1-3]?[0-9]

With this as input

mon19hellotues20
mon19world
hellomon19
tues8worldtues22

I want

mon19tues20
tues8tues22

As output

Upvotes: 0

Views: 38

Answers (1)

anubhava
anubhava

Reputation: 784898

sed is better tool for this to print certain matched txt in output:

sed -nE 's/(mon|tues)([1-3]{0,1}[0-9]).*(mon|tues)([1-3]{0,1}[0-9])/\1\2\3\4/p' file

mon19tues20
tues8tues22

Upvotes: 1

Related Questions