22

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?

3 Answers 3

27

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.

2
  • 5
    or use sed -E 's/(word. ){2}/&\n/g' tmp
    – Cyrus
    Sep 13, 2014 at 8:27
  • -e -E means full regular expression? Making possible to use advanced patterns like {x} ?
    – Sandburg
    Dec 29, 2020 at 15:29
1

Another way in sed:

$ sed 's/ /\x1&\x2/g; s/\([^\x1]*[\x1][^\x2]*[\x2][^\x1]*\)[\x1][^\x2]*[\x2]/\1\
/g; s/[\x1\x2]//g'
2
1

For the general case of matching N times:

$ perl -ple '$N=3;s/(\S+ ){$N}\K/\n/g' tmp
word1 word2 word3 
word4 word5 word6 
word7

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .