$ perl -le '$_="1 2 3 4 5 6"; s/^((?:[^\s]*\s){3}[^\s]*)\s/$1\n/ and print' 1 2 3 4 5 6 #### s/^([^\s]*(?:\s[^\s]*){3})\s/$1\n/ #### s/ ^ # Start at the beginning. ( # Start capturing. [^\s]* # 0 or more non-space. \s [^\s]* # A space and 0 or more non-spaces... once. \s [^\s]* # twice. \s [^\s]* # three times. ) # Stop capturing. \s # Match another space. /$1\n/x # Replace with what we captured and a newline.