in reply to Need help with regex to replace 4th \s with \n in data line

$ perl -le '$_="1 2 3 4 5 6"; s/^((?:[^\s]*\s){3}[^\s]*)\s/$1\n/ and p +rint' 1 2 3 4 5 6

That might be more readable as:

s/^([^\s]*(?:\s[^\s]*){3})\s/$1\n/

or expanded:

s/^([^\s]*\s[^\s]*\s[^\s]*\s[^\s]*)\s/$1\n/

The idea is to match anything that is not a space followed by a space and 0 or more non-spaces 3 times and capture it. Then match another space and replace all of that with what you captures and a newline at the end.

Expanding and commenting gives us:

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.

Of course, that all looks rather ugly with the negated char classes written like that. You should probably use \S instead. So, finally:

s/^(\S*(?:\s\S*){3})\s/$1/n/
-sauoq
"My two cents aren't worth a dime.";