Here is your text
test1 test2 test3 test4
test5 test6 test7
Use C-M-%
(query-replace-regexp
) to group the text by three, i.e.,
;; Type C-q C-j to get <newline>
C-M-% [ <newline>]+ → \,(if (zerop (% (1+ \#) 3)) "\n" "|")
then you'll get
test1|test2|test3
test4|test5|test6
test7|
Use C-x r t
(string-rectangle
) to prepend |
to each line, then you'll get
|test1|test2|test3
|test4|test5|test6
|test7|
Switch to org-mode
and press TAB
, then you'll get
| test1 | test2 | test3 |
| test4 | test5 | test6 |
| test7 | | |
If you want an Emacs command to do the above, you can refer to the code below:
(defun my-org-table-convert-region (b e)
(interactive "*r")
(let ((s (delete-and-extract-region b e))
(old-point (point))
(counter 0))
(insert
(replace-regexp-in-string
(rx (1+ (in "\s\t\n")))
(lambda (_)
(if (zerop (% (cl-incf counter) 3))
"\n"
" "))
(string-trim s)))
(org-table-convert-region old-point (point) " +")))