1

below two examples are expected behaviors. no problems
both "xxx2", "xxx@" match with "xxx." pattern.

$ echo abc1xxx2abc3abc4@111 | sed -rn 's/((abc.|xxx.){3}).*/\1/p'
abc1xxx2abc3

$ echo abc1xxx@abc3abc4@111 | sed -rn 's/((abc.|xxx.){3}).*/\1/p'
abc1xxx@abc3

also below two examples are expected behaviors.
both "xxx@", "xxxy" match with "xxx." pattern
but right next "@" char is not match for pattern so no output

$ echo abc1xxx@@abc3abc4@111 | sed -rn 's/((abc.|xxx.){3}).*/\1/p'
no output

$ echo abc1xxxy@abc3abc4@111 | sed -rn 's/((abc.|xxx.){3}).*/\1/p'
no output

but if i changed the "y" in the above example to "x" then
suddenly pattern is matched even though next char is "@"
I can't understand this behavior

$ echo abc1xxxx@abc3abc4@111 | sed -rn 's/((abc.|xxx.){3}).*/\1/p'
abc1xxxx@abc3abc4

1 Answer 1

2

Let's change your pattern ever so slightly so that we can see what happens (just add ^.* to the beginning so that the whole input line is replaced by the back reference):

$ echo abc1xxxx@abc3abc4@111 | sed -rn 's/^.*((abc.|xxx.){3}).*/\1/p'
xxx@abc3abc4

So it matches xxx@ followed by abc3 followed by abc4. This is perfectly legal and expected since the pattern is looking for three occurrences of either abc. or xxx..

The abc1x at the start of your output line was never affected by the pattern and therefore also never replaced by the substitution. It was just passed on from the input.

1
  • Thanks Kusalananda. I coudn't really think about that. I just think right most greedy pattern ".*" Commented Jan 21, 2017 at 23:28

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.