Sed match pattern N times

sedtext processing

I want to split a file into chunks with 2 words each.

$cat tmp
word1 word2 word3 word4 word5 word6 word7
$sed -e 's/word. word. /&\n/g' tmp
word1 word2 
word3 word4 
word5 word6 
word7
$sed -e 's/word. \{2\}/&\n/g' tmp
word1 word2 word3 word4 word5 word6 word7

I expected the last command to give same result as the one before it. What is wrong?

Best Answer

Sorry, seems like I figured it out just after posting.

It needs to be

sed -e 's/\(word. \)\{2\}/&\n/g' tmp

Apparently the parentheses are needed to let sed apply {2} condition on the entire pattern word. and not just preceding space.

Related Question